Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:09:16+00:00 2026-05-23T22:09:16+00:00

I have a quick question about how smart the Java compiler provided in Sun’s

  • 0

I have a quick question about how “smart” the Java compiler provided in Sun’s JDK is. Specifically, is it smart enough to evaluate any functions appearing in the conditional part of a for() loop ahead of time instead of evaluating them at every iteration of the loop?

For instance, consider the following code.

// Array of doubles to "hold" the array.
private double matrix[][];

public int getCols() {
    // Compute the number of columns in a matrix.
}

public int getRows() { 
    // Compute the number of rows in a matrix.
}

// Compute the sum of all elements in the matrix.
public double sum() {

    double result = 0;

    for (int r = 0; r < getRows(); r++) {
        for (int c = 0; c < getCols(); c++) {
            result += this.matrix[r][c];
        }
    }

    return result;
}

Clearly, I could modify the sum() method to ensure that getRows() and getCols() aren’t evaluated at each iteration of the loop by changing it to

public double sum() {

    double result = 0;
    int numRows = getRows();
    int numCols = getCols();

    for (int r = 0; r < numRows; r++) {
        for (int c = 0; c < numCols; c++) {
            result += this.matrix[r][c];
        }
    }

    return result;
}

I wonder, however, if the compiler is smart enough to pre-evaluate these itself. That is, will it automatically spot that it’s computationally cheaper to evaluate any functions that appear in conditionals ahead of time rather than evaluate them at each iteration?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:09:16+00:00Added an answer on May 23, 2026 at 10:09 pm

    Generally, such an optimization would be wrong. The methods are virtual, so they may be changed in a subclass to do something entirely different (such as returning random numbers) and it may be hard to impossible to statically prove (yes, it needs to be a proof) that they return the same value on every iteration even if they were final. In fact, maybe they don’t do that at all (think another thread changing the number of rows/columns in parallel – yeah, you got an whole lot of other problems in that case, but it still needs to be considered).

    And apart from that, the compiler doesn’t need to optimize anything: At runtime, the JIT-compiler can optimize much more. For instance, it can generate code with the virtual calls inlines and it may (depending on the code) be able to factor out the wrong (at least if they unconditionally return a constant). If can’t, however, change semantics, so again, it won’t do this. If it’s really important, do it yourself. Either way, check if it’s a bottleneck at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quick question about fetching results from a weakly typed cursor and
I have a quick question about adding Zend_Dojo_Form into Zend_layouts. I have a Zend_Dojo_Form
I just have a quick question about IE8 Standards mode vs Quirks mode. The
I have a quick question about setting up the mappings between [Order Details] ,
I have a quick question about programming using Vim. I sometimes make a silly
i have a quick question about having a reference to a subset of a
I have a quick question about mysql_real_escape_string. Where should I use it? I have
I am new to LINQ and EF and I have a quick question about
i have a quick Binding Question about Silverlight. I have some Expander and want
just a quick question: I am a CS undergrad and have only had experience

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.