Community
Participate
Working Groups
When using a trinary ?: expression, the compiler gives a spurious error that the types are incompatible if they are different subtypes. Workaround - cast either the true or false result to abstract class. Version: 2.1 Build id: 200212181304 WinXP home JDK 1.4.1_01 ----test case--- note the last line generates an error as marked. void test(int a){ OutputStream os; FileOutputStream fs = new FileOutputStream("a"); ByteArrayOutputStream bs =new ByteArrayOutputStream(); os = new FileOutputStream("a"); os = new ByteArrayOutputStream(); os = fs; os = bs; os = (a > 5) ? (OutputStream)fs: bs; // OK os = (a > 5) ? fs: (OutputStream)bs; // OK os = (a > 5) ? fs: bs; //Incompatible conditional operand types FileOutputStream and ByteArrayOutputStream }
compiler lives in jdt core
Maybe the message is not very clear, but this code is incorrect. Look at the JLS http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293 * If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (ยง5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type. So like ByteArrayOutputStream cannot be assignment compatible with FileOutputStream, the compile reports a compile-error. This is clearly a valid error. Javac 1.4.1 and jikes 1.18 report the same problem.
Closing, we implement the JLS spec strictly.