+ 3

Java help needed

char b = 'a' + 2; System.out.print(b); I am facing issue while getting correct answer for this. people are giving varying answers and contradicting to the correct one which is provided by sololearn. Please help me with this

1st Oct 2017, 12:03 PM
Ravi Shankar
Ravi Shankar - avatar
7 Answers
+ 3
should be 'c' ... do you need an explanation why ? or what is the question ^^
1st Oct 2017, 12:13 PM
Chrizzhigh
Chrizzhigh - avatar
+ 4
You're getting that because it's theĀ ASCII valueĀ of the char. charĀ is actually an integer type. It stores the 16-bit Unicode integer value of the character in question. You can look at something likeĀ http://asciitable.comĀ to see the different values for different characters.
1st Oct 2017, 12:12 PM
Alijany Mohammad H
Alijany Mohammad H - avatar
+ 3
alijany already said it, it is because a char (eg. 'a') instead of a string (eg "a") has a fix ASCII value. by adding a numeric value to it you get the next ASCII value in the line ... so bei adding 2 to the value of a you get c. you can google ascii table to see some examples and play around with it
1st Oct 2017, 12:19 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
here is somthing to work with.^^ public class Program { public static void main(String[] args) { int a = 'a'; char b = 98; System.out.println(a); // prints value of 'a' System.out.println(b); // prints char of value 98 } }
1st Oct 2017, 12:25 PM
Chrizzhigh
Chrizzhigh - avatar
0
sorry idont understand. i dont know the answer too. please help me with that also
1st Oct 2017, 12:14 PM
Ravi Shankar
Ravi Shankar - avatar
0
yes, an explanation would be great.Thanks
1st Oct 2017, 12:15 PM
Ravi Shankar
Ravi Shankar - avatar
0
Thank you all
1st Oct 2017, 12:38 PM
Ravi Shankar
Ravi Shankar - avatar