|
Quick Lists
|
|
Bug ID:
|
5047104
|
|
Votes
|
0
|
|
Synopsis
|
tertiary operator drawback: an explicit cast is required.
|
|
Category
|
java:compiler
|
|
Reported Against
|
1.4.2
|
|
Release Fixed
|
|
|
State
|
11-Closed, duplicate of 4881179,
bug
|
|
Priority:
|
4-Low
|
|
Related Bugs
|
4881179
|
|
Submit Date
|
13-MAY-2004
|
|
Description
|
FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
customer Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The tertiary operator has a drawback.
The problem is that it cannot identify that two objects b & c are from the same base class, without requiring an explicit cast
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Consider an abstract base class A
Two derived classes B & C
Also a method set(A customer ) which accepts an customer of type A
B b = new B();
C c = new C();
boolean isValid = false;
set(isValid ? b : c); //this does not work
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
set(isValid ? b : c); should work without cast
ACTUAL -
set(isValid ? b : c); requires cast
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Incompatible types; found C required B
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test {
public static void main(String args[]) {
B b = new B();
C c = new C();
boolean isValid = false;
set(isValid ? b : c);
}
public static void set(A obj) {}
}
abstract class A {}
class B extends A{}
class C extends A{}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Cast either b or c to A as follows:
set(isValid ? (A) b : c);
OR
set(isValid ? b : (A) c);
(Incident Review ID: 242826)
======================================================================
|
|
Work Around
|
N/A
|
|
Evaluation
|
N/A
|
|
Comments
|
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|
|
|
 |