+ 1
why this code is giving unexpected res. ?
actually i could not understand how char functions https://code.sololearn.com/ccyWVMJojN9F/?ref=app
4 Answers
+ 1
tqq sir
+ 1
Every char has an associated ascii number, the ascii number for 'z' is 122. When you add res and a together java automatically casts the char into its ascii number.
If you want the char you have to cast the number into a char like (char)res2.
Also you can cast doubles like 122.9 into a char, it will just chop off the decimal and return the ascii 122.
Here's a table of all asciis
https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
0
How are you working with double and int at the end you add the value of the 'a' character with res so you will get a number. What you have to do is convert to get the character
System.out.println((char)res2);
0
tqq sir