Ich frage mich, was die Logik dahinter ist und warum der Compiler ein Downcasting zulässt während man das andere verhindert? Es scheint logisch und korrekt zu sein, warum also diese Einschränkung?
Der folgende Code funktioniert und lässt sich gut kompilieren
Code: Select all
public class Testing {
public static void main(String[] args) {
final int x = 99;
byte y = 33;
byte r = true ? x : y;
System.out.println(r);
}
}
Code: Select all
public class Testing {
public static void main(String[] args) {
final short x = 99;
byte y = 33;
byte r = true ? x : y;
// java: incompatible types: possible lossy conversion from short to byte
System.out.println(r);
}
}