-
Bug
-
Resolution: Unresolved
-
P4
-
8, 11, 17, 23, 24, 25
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
The OS is Windows 11.
The program is compiled using javac.exe directly from the cmd.
The behavior is the same on JDK 8, 11, 17, 21, 23, and newest release 25.
A DESCRIPTION OF THE PROBLEM :
The JLS §15.25.2 – Numeric Conditional Expressions states:
"If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T."
The JLS §15.28 – Constant Expressions states:
"A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
...
- The ternary conditional operator ? : (§15.25)
..."
The JLS §5.2 – Assignment Contexts states:
"In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:
- A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable."
From the above sections there are two conflicting interpretations.
1. From 15.25.2, in conditional expression the if one expression is of type char, byte, short and the other one is of "larger" type, the the resulting type should be expanded to match the bigger one.
2. From 15.28 and 5.2 if one expression is of type char, byte, short and the other one is of "larger" type but "fits" in the type of the first one, then the resulting type should be narrowed to match the smaller one.
Detailed explanation and reason for filing the bug report are explained in the answer to this question: https://stackoverflow.com/questions/79352451/why-in-java-conditional-expression-the-narrowing-conversion-of-return-type-only
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use conditional expression where one expression is a final short and the other is a byte or char. (Alternatively first is a final short or final byte and the other is char). Then try to assign the value of the conditional expression to the variable of type byte (or char in the alternative case).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program compiles correctly.
ACTUAL -
Compilation error: "java: incompatible types: possible lossy conversion from short to byte"
---------- BEGIN SOURCE ----------
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);
}
}
---------- END SOURCE ----------
FREQUENCY : always
The OS is Windows 11.
The program is compiled using javac.exe directly from the cmd.
The behavior is the same on JDK 8, 11, 17, 21, 23, and newest release 25.
A DESCRIPTION OF THE PROBLEM :
The JLS §15.25.2 – Numeric Conditional Expressions states:
"If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T."
The JLS §15.28 – Constant Expressions states:
"A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
...
- The ternary conditional operator ? : (§15.25)
..."
The JLS §5.2 – Assignment Contexts states:
"In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:
- A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable."
From the above sections there are two conflicting interpretations.
1. From 15.25.2, in conditional expression the if one expression is of type char, byte, short and the other one is of "larger" type, the the resulting type should be expanded to match the bigger one.
2. From 15.28 and 5.2 if one expression is of type char, byte, short and the other one is of "larger" type but "fits" in the type of the first one, then the resulting type should be narrowed to match the smaller one.
Detailed explanation and reason for filing the bug report are explained in the answer to this question: https://stackoverflow.com/questions/79352451/why-in-java-conditional-expression-the-narrowing-conversion-of-return-type-only
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use conditional expression where one expression is a final short and the other is a byte or char. (Alternatively first is a final short or final byte and the other is char). Then try to assign the value of the conditional expression to the variable of type byte (or char in the alternative case).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program compiles correctly.
ACTUAL -
Compilation error: "java: incompatible types: possible lossy conversion from short to byte"
---------- BEGIN SOURCE ----------
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);
}
}
---------- END SOURCE ----------
FREQUENCY : always