How does each loop work inside JAVA?

I tried to find work for each cycle when I make a function call. See the following code,

public static int [] returnArr()
{
    int [] a=new int [] {1,2,3,4,5};
    return a;
}

public static void main(String[] args)
{
    //Version 1
    for(int a : returnArr())
    {
        System.out.println(a);
    }

    //Version 2
    int [] myArr=returnArr();
    for(int a : myArr)
    {
        System.out.println(a);
    }
}

In version 1, I call the returnArr () method for each loop, and in version 2 I explicitly call the returnArr () method and assign it to an array, and then repeat it. The result is the same for both scenarios. I would like to know what is more effective and why.

I thought version 2 would be more efficient since I do not call the method at each iteration. But, to my surprise, when I debugged code using version 1, I saw that the method was called only once!

Can someone explain how this works? ? What is more efficient / better when I code complex objects?

+3
3

Java Language Specification, , .

:

for (T x : expr) {
    // do something with x
}

expr , , , - , :

T[] arr = expr;
for (int i = 0; i < arr.length; i++) {
    T x = arr[i];
    // do something with x
}

, arr i - , , . : , , .

expr , , expr. , , JIT, .

+1

Java

L1 ... Lm - (, ) .

for:

T[] #a = Expression;
L1: L2: ... Lm:
for (int #i = 0; #i < #a.length; #i++) {
    {VariableModifier} TargetType Identifier = #a[#i];
    Statement
}

Expression : ( returnArr()). : 1, for; 2, , .

+4

The compiler calls the method returnArr()only once. optimization of compilation time :)

Bytecode:

 public static void main(java.lang.String[]);
   descriptor: ([Ljava/lang/String;)V
   flags: ACC_PUBLIC, ACC_STATIC
   Code:
     stack=2, locals=6, args_size=1

** case -1  start ***
        0: invokestatic  #20                 // Method returnArr:()[I  --> called only once. 
        3: dup
        4: astore        4
        6: arraylength
        7: istore_3
        8: iconst_0
        9: istore_2
       10: goto          28
       13: aload         4    --> loop start
       15: iload_2
       16: iaload
       17: istore_1
       18: getstatic     #22                 // Field java/lang/System.out:Ljav
/io/PrintStream;
       21: iload_1
       22: invokevirtual #28                 // Method java/io/PrintStream.prin
ln:(I)V
       25: iinc          2, 1
       28: iload_2
       29: iload_3
       30: if_icmplt     13


***case -2  start****

       33: invokestatic  #20                 // Method returnArr:()[I
       36: astore_1
       37: aload_1
       38: dup
       39: astore        5
       41: arraylength
       42: istore        4
       44: iconst_0
       45: istore_3
       46: goto          64
       49: aload         5   --> loop start case 2
       51: iload_3
       52: iaload
       53: istore_2
       54: getstatic     #22                 // Field java/lang/System.out:Ljav
/io/PrintStream;
       57: iload_2
       58: invokevirtual #28                 // Method java/io/PrintStream.prin
ln:(I)V
       61: iinc          3, 1
       64: iload_3
       65: iload         4
       67: if_icmplt     49
       70: return

Note. I am using jdk 8.

+1
source

Source: https://habr.com/ru/post/1653391/


All Articles