java - Why is my char printing as a number instead of a character? -
as per java ternary operator expression ? statement1 : statement2
, if expression
true statement1
executed, if expression
false statement2
executed.
but when run:
// unnecessary codes not displaying char y = 'y'; int = 0; system.out.print(false ? : y);
i expecting print y
printing 121
, why?
edit per manouti answer, compiler interprets int
, if case why seeing dead code @ i
?
if system.out.print(false ? 0 : x);
getting y
, why in case doesn't compiler interpret int
?
the short answer question value printed based on type conditional expression evaluates to.
so question boils down to, why type of conditional expression differ between
char y = 'y'; int = 0; system.out.print(false ? : y); // prints 121
and
char y = 'y'; system.out.print(false ? 0 : y); // prints y
to answer that, we'll need take @ section §15.25 of java language specification.
there 3 types of conditional expression in java:
- boolean conditional expressions
- numeric conditional expressions
- reference conditional expressions
since both int
, char
convertible numeric type, expression example of numeric conditional expression according rule:
if both second , third operand expressions numeric expressions, conditional expression numeric conditional expression.
for purpose of classifying conditional, following expressions numeric expressions:
- an expression of standalone form (§15.2) type convertible numeric type (§4.2, §5.1.8).
given that, rule determining type of entire expression given follows:
15.25.2. numeric conditional expressions
numeric conditional expressions standalone expressions (§15.2).
the type of numeric conditional expression determined follows:
if second , third operands have same type, type of conditional expression.
if 1 of second , third operands of primitive type t, , type of other result of applying boxing conversion (§5.1.7) t, type of conditional expression t.
if 1 of operands of type byte or byte , other of type short or short, type of conditional expression short.
if 1 of operands of type t t byte, short, or char, , other operand constant expression (§15.28) of type int value representable in type t, type of conditional expression t.
if 1 of operands of type t, t byte, short, or character, , other operand constant expression of type int value representable in type u result of applying unboxing conversion t, type of conditional expression u.
otherwise, binary numeric promotion (§5.6.2) applied operand types, , type of conditional expression promoted type of second , third operands.
note binary numeric promotion performs value set conversion (§5.1.13) , may perform unboxing conversion (§5.1.8).
notice fourth rule describes second example; second operand constant of type int
(0
) , third char
, conditional expression evaluate char
. cause compiler use print(char)
method, print y
.
however when instead pass in variable instead of constant, fall down last rule says "...the type of conditional expression promoted type of second , third operands."
if take @ section §5.6.2 of jls, describes rules type promotion follows:
when operator applies binary numeric promotion pair of operands, each of must denote value convertible numeric type, following rules apply, in order:
if operand of reference type, subjected unboxing conversion (§5.1.8).
widening primitive conversion (§5.1.2) applied convert either or both operands specified following rules:
if either operand of type double, other converted double.
otherwise, if either operand of type float, other converted float.
otherwise, if either operand of type long, other converted long.
otherwise, both operands converted type int.
by following these rules, type of expression int
, , compiler use print(int)
method, print 121
(the ascii value of y
).
Comments
Post a Comment