Duplicate :
|
Name: vpR10072 Date: 11/12/2003 The "Autoboxing and Auto-unboxing support for Java^tm Programming Language" document reads in JLS 15.25 Conditional Operator ? : ... The conditional operator may be used to choose between second and third operands of types that are convertible to numeric type, or second and third operands of type boolean or Boolean, or second and third operands that are each of either reference type or the null type. However javac reports errors if reference type operands are used in conditional operator. To reproduce: ------------- Consider a testcase with two fragments: * when the second operand is of type boolean and the third operand is of type Boolean, * when the second operand is of type byte and the third operand is of type Byte % cat bug.java -----------------------------------------------cut here class bug { public static void main (String args[]) { boolean t = true; Boolean F = new Boolean(false); byte b = (byte)2; Byte B = new Byte((byte)1); if (! (t ? t : F)) System.out.println("FAIL"); if ( (t ? b : B) != b ) System.out.println("FAIL"); } } -----------------------------------------------cut here % java -showversion java version "1.5.0-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b26) Java HotSpot(TM) Client VM (build 1.5.0-beta-b26, mixed mode) % javac -source 1.5 bug.java bug.java:8: incompatible types for ?: neither is a subtype of the other second operand: boolean third operand : java.lang.Boolean if (! (t ? t : F)) ^ bug.java:11: incompatible types for ?: neither is a subtype of the other second operand: byte third operand : java.lang.Byte if ( (t ? b : B) != b ) ^ 2 errors ======================================================================