Java for Loop evaluation

前端 未结 9 2278
孤独总比滥情好
孤独总比滥情好 2020-12-03 21:45

I want to know if the condition evaluation is executed in for and while loops in Java every time the loop cycle finishes.

Example:

相关标签:
9条回答
  • 2020-12-03 22:07

    If the conditional expression is a loop invariant and you are running in JIT mode, loop optimizations such as Loop-invariant code motion can be done.

    But when run in the interpreted mode, I guess there is not much optimization to do.

    0 讨论(0)
  • 2020-12-03 22:11

    index < tenBig.length will be execute before every time the loop cycle starts.

        public static void main(String[] args) {
            int[] tenBig = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
            for (int index = 0; isEvaluated() && index < tenBig.length; index++) {
                System.out.println("Value at index: " + tenBig[index]);
            }
        }
    
        public static boolean isEvaluated() {
            System.out.println("evaluated");
            return true;
        }
    

    It will print "evaluated" just before cycles starts. And one more time before loop finishes.

    0 讨论(0)
  • 2020-12-03 22:13

    It's going to be executed everytime the loop is entered, including the last evaluation which will yield index < length = false . Aside from this, even though the length of tenBig is const, the loop will always access this property so it would be ideal to assign it to a variable (even though it's not a reasonable speed gain in your example).

    0 讨论(0)
  • 2020-12-03 22:14

    Yes, the expression must be evaluated for each iteration of the loop to determine whether to stay inside the loop or continue program execution.

    Remember, you can do things like this:

    for(int a = 0, z = 26; a < z; a++, z--){
        // Do some work
    }
    

    In that case, both sides of the expression will change and must be evaluated for each iteration.

    And you're right, if you have calculations in the for loop condition that can be moved out to a separate variable, you should do that:

    for(int i = 0; i < ((someCount / 2) * 21.6); i++){
    }
    

    Could easily be:

    int maxVal = (someCount / 2) * 21.6;
    
    for(int i=0; i < maxVal; i++){
    {
    
    0 讨论(0)
  • 2020-12-03 22:20

    Here are several bytecode compiling examples from the JVM spec.

    As far as I know, the condition will be evaluated every time.

    Regards.

    0 讨论(0)
  • 2020-12-03 22:22

    Yes, it will logically evaluate the whole of the middle operand on every iteration of the loop. In cases where the JIT knows better, of course, it can do clever things (even potentially removing the array bounds check within the loop, based on the loop conditions).

    Note that for types that the JIT doesn't know about, it may not be able to optimize specifically like this - but may still inline things like fetching the size() of an ArrayList<T>.

    Finally, I generally prefer the enhanced for loop for readability:

    for (int value : tenBig) {
        ...
    }
    

    Of course, that's assuming you don't need the index for other reasons.

    0 讨论(0)
提交回复
热议问题