Java Reference
In-Depth Information
}
}
class Tails extends CoinSide {
private Tails() { }
public static final Tails INSTANCE = new Tails();
public String toString() {
return "tails";
}
}
Solution 75: Heads or Tails?
This program doesn't appear to use any release 5.0 features at all, so it is difficult to see why there
should be any difference in behavior. In fact, the program fails to compile in release 1.4 or any
earlier release:
CoinSide.java:7:
incompatible types for ?: neither is a subtype of the other
second operand: Heads
third operand : Tails
return rnd.nextBoolean() ?
^
The behavior of the conditional ( ? : ) operator was more restrictive before release 5.0 [JLS2 15.25].
When both the second and third operands were of a reference type, the conditional operator
required that one of them be a subtype of the other. As neither class Heads nor Tails is a subtype of
the other, there is an error. To get this code to compile, you could cast one of the operands to the
 
 
Search WWH ::




Custom Search