Reputation: 109
In Java, you can cast char
to int
and vice-versa, using the char value or the ASCII value. If you cast an int variable to a char, you get the corresponding character. So, the following snippet will print 'a':
int x = 97;
System.out.println( (char)x ); // 'a'
But when I do this:
char ch = 'a', ch2 = 97, ch3 = 'b';
System.out.println( ( (ch+=1) > ch2 ) ? (char)ch2 : (int)ch3 );
the compiler prints out the int value 97, not 'a', even though the ternary operator return value on the 'true side' is (char)ch2. So I expected 'a' instead of 97. Why does it print 97 instead of 'a'?
Upvotes: 2
Views: 579
Reputation: 51623
The problem is that in:
System.out.println(((ch += 1) > ch2) ? (char) ch2 : (int) ch3);
because you have (int) ch3
the compiler assumes that the returning type of the ternary operator will be an int
.
Check the highlighted part of the rule from JLS 15.25. Conditional Operator ? :
The type of a conditional expression is determined as follows:
If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.
If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
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.
If one of the operands is of type T, where T is Byte, Short, or Character, and the other operand is a constant expression (§15.28) of type int whose value is representable in the type U which is the result of applying unboxing conversion to T, then the type of the conditional expression is U.
Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.
If you do
System.out.println(((ch += 1) > ch2 ) ? (char) ch2 : (char) ch3);
it will print
'a'
Alternatively, you can apply brute-force i.e. cast the final result into char
as shown below:
System.out.println((char) (((ch += 1) > ch2 ) ? (char) ch2 : (int) ch3));
it will also print
'a'
Upvotes: 3