Community
Participate
Working Groups
These are not equivalent in the Eclipse compiler: int x = 2; System.out.println("n: "+(x > 1 ? 2 : 1.0)); => n: 2.0 and System.out.println("n: "+(true ? 2 : 1.0)); => n: 21 This is incorrect according to http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
The compiler also allows this code, which the Sun compiler rejects: System.out.println("n: "+(x > 1 ? "x" : 1));
Reproduced case from comment 0 with HEAD. Case from comment 1 doesn't fail with java 1.5 (-source 1.5), but fails with javac 1.5 (-source 1.4). With compliance 1.5 and source 1.4, we also report: ---------- 1. ERROR in D:\tests_sources\X.java (at line 4) System.out.println("n: "+(x > 1 ? "x" : 1)); ^^^^^^^^^^^^^^^^^ Incompatible conditional operand types String and int ---------- 1 problem (1 error) So only the first case is an issue.
Created attachment 30422 [details] Proposed fix The problem in the first case comes from the constant computation. We have the right type (double) for the constant of the conditional expression, but we simply get the stringValue() of the int constant (2). Philippe, could you please review the proposed fix?
All existing tests passed with this patch.
Fixed and released in HEAD. Added regression tests org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test017/018.
Pls backport to 3.1 stream.
Backporter to 3.1 maintenance. Regression tests added to org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test014/015.
Backported to 3.0 maintenance. Regression tests added to org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test012/013.
Verified for 3.2 M4 using build I20051212-0010
Reopen for 3.1.2 verification
We also need to verify that this bug is fixed using R3_1_maintenance build as well before 3.1.2 delivery...
Verified for 3.1.2 using build M20060109-1200.
Verified for 3.1.2 using build M20060109-1200 (maxime)