JDK-4952864 : javac problems in conditional operator on reference type values
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_8
  • CPU: generic
  • Submitted: 2003-11-12
  • Updated: 2003-11-12
  • Resolved: 2003-11-12
Related Reports
Duplicate :  
Description

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

======================================================================