Differences in Java for Loop

Are these statements exactly the same in terms of memory usage and memory efficiency in Java?

First

Object[] array = new Object[100]; int i = 0; for (; i < array.length; i++){ Object o = array[i]; //do something } 

Second

 Object[] array = new Object[100]; for (int i = 0; i < array.length; i++){ Object o = array[i]; //do something } 

Third

 Object[] array = new Object[100]; for (Object o : array){ //do something } 
+2
source share
4 answers

In terms of employment and memory efficiency, yes. However, there are differences. In the first case, i exists (has a scope) outside the loop; and in the second - no. In the third case, there is no (direct) way to access the index or to change the contents of the array at the position of the current object.

+6
source

The first is not an ordinary idiom; I wouldn’t write like that.

There is no difference in memory or effectiveness. The third is syntactic sugar, which was added to a later JVM (I believe this is JDK 6).

Your code will be the bottleneck of memory and efficiency, not the lines of the loop.

+1
source

The third version was introduced with Java 5, designed to simplify your work with generics. It is improved because you do not need to determine how many elements are in the array before the loop. There is also no need to specify how to increase the current position, providing a cleaner implementation without the need to create a counter variable or Iterator.

0
source

No, this is not exactly the same. And it is easily verifiable and imho is not even surprising.

Just decompile the following two functions:

 public static void test1(Object[] arr) { for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } public void test2(Object[] arr) { for(Object o : arr) { System.out.println(o); } } 

and look at the output:

 public static void test1(java.lang.Object[]); Code: 0: iconst_0 1: istore_1 2: iload_1 3: aload_0 4: arraylength 5: if_icmpge 23 8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream; 11: aload_0 12: iload_1 13: aaload 14: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V 17: iinc 1, 1 20: goto 2 23: return public void test2(java.lang.Object[]); Code: 0: aload_1 1: astore_2 2: aload_2 3: arraylength 4: istore_3 5: iconst_0 6: istore 4 8: iload 4 10: iload_3 11: if_icmpge 34 14: aload_2 15: iload 4 17: aaload 18: astore 5 20: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream; 23: aload 5 25: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V 28: iinc 4, 1 31: goto 8 34: return } 

I just turned on println () to see something done with the variable and make sure javac is not optimizing it. Obviously, in the larger picture, the difference does not matter, and they can hardly be measurable, but still, not the same code;)

Although I'm not sure what exactly is happening in the second function, so if someone wants to spend time and analyze the code, continue :-)

0
source

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


All Articles