Bug 77604

Summary: Conditional assignment error
Product: [Eclipse Project] JDT Reporter: Eric Bodden <eric>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3    
Version: 3.0   
Target Milestone: 3.1 M3   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Eric Bodden CLA 2004-11-03 07:56:42 EST
Hi.

I am not sure, whether that is a compiler bug or if just the Java language spec 
is a bit flawed. I tried to do the following:

public class Test1 {

	static class A{}
	static class B extends A{}
	static class C extends A{}
	
	
	public static void main(String[] args) {
		A a = false ? new B() : new C();
	}
}

Javac compiles fine here. Eclipse however tells that B and C do not have equal 
types, which is correct, but actually unnecessary, because they both have a 
common supertype A.

The following also compiles fine in Eclipse:

public class Test1 {

	static class A{}
	static class B extends A{}
	static class C extends A{}
	
	
	public static void main(String[] args) {
		A a;
		
		if(false)
			a = new B();
		else
			a = new C();
	}
}
Comment 1 Philipe Mulet CLA 2004-11-03 08:34:25 EST
Javac 1.4.2 issues an error as well:
X.java:9: incompatible types for ?: neither is a subtype of the other
second operand: X.B
third operand : X.C
                                 A a = false ? new B() : new C();
                                             ^
1 error

I suspect you did compare to javac 1.5.0, which now accepts it, like we do when
toggle into 1.5 compliant mode.
Please reopen if wrong.