0
Char near int - please help me understand
Hello there, Im starting my adventure with java and Ive done a trial and I wanted to declare three new variables - int x=2 char y=s and string z=yo After puting system in println(x + y + z) and running program I got 117yo, but when I put (x + " " + y + z) I get what I wanted which is 2 syo Question I wanted to ask is why after compliling I get 117 when there is no space between x and y?
1 Resposta
+ 3
Because x + y is adding 2 to the ASCII value of the character 's' (115), which is 117.
There are a few ways you could concatenate it, but the simplest would be the way you already have or by explicitly telling the compiler that y is a character data-type by casting it like this:
(x + (char)y + z)