why this code is giving unexpected res. ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why this code is giving unexpected res. ?

actually i could not understand how char functions https://code.sololearn.com/ccyWVMJojN9F/?ref=app

11th Oct 2019, 4:23 PM
Rohit Raj
Rohit Raj - avatar
4 Answers
+ 1
tqq sir
11th Oct 2019, 4:35 PM
Rohit Raj
Rohit Raj - avatar
+ 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
11th Oct 2019, 4:40 PM
Odyel
Odyel - avatar
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);
11th Oct 2019, 4:33 PM
Stiven 98
Stiven 98 - avatar
0
tqq sir
11th Oct 2019, 4:42 PM
Rohit Raj
Rohit Raj - avatar