Why is this loop changed?

  • A+
Category:Languages

I just encountered this decompiled class file of my class:

MyClass

while ((line = reader.readLine()) != null) {     System.out.println("line: " + line);     if (i == 0) {         colArr = line.split(Pattern.quote("|"));      } else {         i++;     } } 

The while loop has been changed to a for loop in the class file:

Decompiled MyClass

for (String[] colArr = null; (line = reader.readLine()) != null; ++i) {     System.out.println("line: " + line);     if (i == 0) {         colArr = line.split(Pattern.quote("|"));     } else {     } } 

Why has this loop been changed to a for? I think it might be another way of code optimization by the compiler, I could be wrong. I just wanted to know if it is, what advantages does a for loop provide over a while loop or other loop?
What is the category of such code optimizations?

 


In this situation changing while() to for() is not an optimization. There is simply no way to know from bytecode which one was used in a source code.

There are many situations when:

while(x) 

is the same as:

for(;x;) 

Suppose we have a two similar java applications - one with for() statement, and one with corresponting while()

APPLICATION #1 - SOURCE

public class For{     public static void main(String args[]) {         int i = 0;         for(; i<5 ;){             System.out.println(i);             i++;         }     } } 

APPLICATION #2 - SOURCE

public class While{     public static void main(String args[]) {         int i = 0;         while(i<5){             System.out.println(i);             i++;         }     } } 

If we compile both of them we have got:

APPLICATION #1 - BYTECODE

public class For {   public For();     Code:        0: aload_0        1: invokespecial #1                  // Method java/lang/Object."<init>":()V        4: return    public static void main(java.lang.String[]);     Code:        0: iconst_0        1: istore_1        2: iload_1        3: iconst_5        4: if_icmpge     20        7: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;       10: iload_1       11: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V       14: iinc          1, 1       17: goto          2       20: return } 

APPLICATION #2 - BYTECODE

public class While {   public While();     Code:        0: aload_0        1: invokespecial #1                  // Method java/lang/Object."<init>":()V        4: return    public static void main(java.lang.String[]);     Code:        0: iconst_0        1: istore_1        2: iload_1        3: iconst_5        4: if_icmpge     20        7: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;       10: iload_1       11: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V       14: iinc          1, 1       17: goto          2       20: return } 

So you can see, there is no difference associated with for and while usage.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: