A DESCRIPTION OF THE REQUEST :
The compiler (javac) is unable to constant fold the expression: ("" != null).
JUSTIFICATION :
Some automatic tools (like javacc) generate code with a conditional that look something like (this is a simplified case -- the two branches are chunky pieces of code in the case of javacc):
public static void main(String[] args) {
if ("" != null)
System.out.println("hi");
else
System.out.println("there");
}
For the above, javac generates the following byte code:
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String
2: ifnull 16
5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
8: ldc #4 // String hi
10: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
13: goto 24
16: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
19: ldc #6 // String there
21: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
24: return
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
For this code one would expect to get the far simpler:
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String hi
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
Javac can generate the above if the "if statement" is changed to "if (true)...".
Probably hotspot can do the optimization later on (though I have no way of checking that) but not doing it early results in excess bytecode that has to carried through the compilation pipeline. Moreover most likely the interpreter never does the optimization resulting is a slower "slow path".
---------- BEGIN SOURCE ----------
public class Foo {
public static void main(String[] args) {
if (true)
System.out.println("hi");
else
System.out.println("there");
}
}
---------- END SOURCE ----------
The compiler (javac) is unable to constant fold the expression: ("" != null).
JUSTIFICATION :
Some automatic tools (like javacc) generate code with a conditional that look something like (this is a simplified case -- the two branches are chunky pieces of code in the case of javacc):
public static void main(String[] args) {
if ("" != null)
System.out.println("hi");
else
System.out.println("there");
}
For the above, javac generates the following byte code:
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String
2: ifnull 16
5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
8: ldc #4 // String hi
10: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
13: goto 24
16: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
19: ldc #6 // String there
21: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
24: return
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
For this code one would expect to get the far simpler:
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String hi
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
Javac can generate the above if the "if statement" is changed to "if (true)...".
Probably hotspot can do the optimization later on (though I have no way of checking that) but not doing it early results in excess bytecode that has to carried through the compilation pipeline. Moreover most likely the interpreter never does the optimization resulting is a slower "slow path".
---------- BEGIN SOURCE ----------
public class Foo {
public static void main(String[] args) {
if (true)
System.out.println("hi");
else
System.out.println("there");
}
}
---------- END SOURCE ----------