Why does ('1'+'1') output 98 in Java?
In java, every character literal is associated with an ASCII value which is an Integer
.
You can find all the ASCII values here
'1'
maps to ASCII value of 49 (int
type).
thus '1'
+ '1'
becomes 49 + 49
which is an integer 98.
If you cast this value to char
type as shown below, it will print ASCII value of 98 which is b
System.out.println( (char) ('1'+'1') );
If you are aiming at concatenating 2 chars (meaning, you expect "11"
from your example), consider converting them to string first. Either by using double quotes, "1" + "1"
or as mentioned here .
'1'
is a char
literal, and the +
operator between two char
s returns an int
. The character '1'
's unicode value is 49, so when you add two of them you get 98.
'1'
denotes a character and evaluates to the corresponding ASCII value of the character, which is 49 for 1
. Adding two of them gives 98.