java - Why I am returning integers instead of characters in the 3rd and 4th print statement? -
can please explain what's going in last 2 print statements? that's lost.
public class { public static void main(string[] args){ char whatever = '\u0041'; system.out.println( '\u0041'); //prints expected system.out.println(++whatever); //prints b expected system.out.println('\u0041' + 1); //prints 66 understand unicode of 1 adds //unicode representing 66 why returning integer when in previous statement returned char? system.out.println('\u0041' + 'a'); //prints 130 wanted show adding //integer unicode in previous print statement not implicit casting because //here add char not implicitly cast char on returned value } }
this happens because of binary numeric promotion
when operator applies binary numeric promotion pair of operands, each of must denote value convertible numeric type, following rules apply, in order, using widening conversion (§5.1.2) convert operands necessary:
- if of operands of reference type, unboxing conversion (§5.1.8) performed. then:
- 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.
basically, both operands converted int
, , system.out.println(int foo)
called. types can returned +
, *
, etc. double
, float
, long
, , int
Comments
Post a Comment