Bug 77604 - Conditional assignment error
Summary: Conditional assignment error
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M3   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-03 07:56 EST by Eric Bodden CLA
Modified: 2004-11-03 08:34 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.