Please explain me how the output is' b'? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

Please explain me how the output is' b'?

char x='A' char y='a' char z='B' z+=y-x Output is 'b' Explain the logic behind the output.How arithmetic calculation is done with char type?

8th Mar 2019, 4:16 PM
Ayush Pandey
Ayush Pandey - avatar
16 Answers
+ 40
You can use this snippet to identify what is happening char x='A'; char y='a'; char z='B'; System.out.println((int)x+" "+(int)y+(int)z); // 65 97 66 z+=y-x; // 66 += 97 - 65 // += 32 // = 98 System.out.println((char)98); When performing math on characters, the chars are converted to it's equivalent ascii value and then perform the expression From the code I mentioned we can see that 'A' = 65, 'a' = 97 and 'B ' = 66 After the calculations variable z store the equivalent char of the value 98 - which is the char 'b'. Therefore it will output b when printing
8th Mar 2019, 4:29 PM
Seniru
Seniru - avatar
+ 29
The key is to understand that characters are encoded in binary (using ascii). So each character has its own code (or number). And since the lower-case (as well as the upper-case) characters' codes are successively sorted, the difference between 'a' and 'A' has the same value of the difference between 'b' and 'B'.
9th Mar 2019, 10:36 AM
Hicham BOURHIL
Hicham BOURHIL - avatar
+ 19
Whenever you add chars, the ascii codes get added so: Ascii code of 'a' = 97 Ascii code of 'A' = 65 Therefore, y - x = 97 - 65 = 32 Now z = 'B' = 66 Therefore, 66 + 32 = 98 = 'b'(ascii code of 'b')
8th Mar 2019, 4:27 PM
Cool Codin
Cool Codin - avatar
+ 15
You need to know two things to understand what's happening here: 1. Every char corresponds to a decimal value, see https://www.rapidtables.com/code/text/ascii-table.html 2. Internally, the decimal values are used to calculate the result. Step by step: char A = dec 65 char a = dec 97 char B = dec 66 y-x = 97-65 = 32 z+(y-x) = 66+32 = 98 dec 98 = char b
8th Mar 2019, 4:32 PM
Tashi N
Tashi N - avatar
+ 7
RISHABH MISHRA Please don’t copy other people’s answers. Please remove your copy.
9th Mar 2019, 5:55 AM
Rowsej
Rowsej - avatar
+ 6
Every number is represented in the binary number system. While characters are represented in ASCII(American Standard Code For Information Interchange ) which is a 7-bit code. ASCII has set values of various symbols in Keyboard including alphabets. Now coming to your question Here char x = 'A' will store 65(ASCII value of A) char y='a' will store 97(ASCII value of a) (Note: upper case and lower case have different ASCII values A-Z 65-90 and a-z 97-122) char z='B' will store 66(ASCII value of B) z += y-x Can be written as z = z + y - x Now putting values z = 66 + 97 -65 z = 98 Which is ASCII value of 'b' So output is b
8th Mar 2019, 7:56 PM
HITESH AHUJA
HITESH AHUJA - avatar
+ 6
Thanks everyone Now from where can I get ascii codes for all the keys and symbols of keyboard.
9th Mar 2019, 5:12 AM
Ayush Pandey
Ayush Pandey - avatar
+ 4
You need to be familiar with data types and Ascii codes
10th Mar 2019, 2:40 AM
Da2
Da2 - avatar
+ 3
In Java,if you try to add two characters,the ASCII values of that character are added.so the ASCII value of A=65,a=97 and B=65.so 66+97-66=98. character with ASCII value of 98 is b
9th Mar 2019, 12:07 AM
Home Number
Home Number - avatar
9th Mar 2019, 9:38 AM
Home Number
Home Number - avatar
+ 2
Ascii value of A = 65 Ascii value of B = 66 Ascii value of a = 97 z+=y-x --> z=z+y-x --> z=66+97-65 --> z=98 Ascii value of b = 98 --> z= b
10th Mar 2019, 7:09 AM
Nishchit Rao
Nishchit Rao - avatar
+ 2
All the values stored in variable x,y,z are the ASCII value for each character. And when you perform an arithmetic then it solve it in mathematical form and gives you 98 as output which is ASCII value of b That's why b is the answer.
13th Mar 2019, 3:27 AM
Sawan Kumar Singh
Sawan Kumar Singh - avatar
9th Mar 2019, 7:58 AM
spkruglov
+ 1
X = A ASCII value 65 and Y=for a ASCII value is 97 Z = For B ASCII value is66 Given That the code is z+=y-x That means z=z+y-x Z= 66+97-65 Z=98 ASCII value of b is 98
10th Mar 2019, 8:43 AM
supriya
+ 1
Its ans is b
12th Mar 2019, 6:18 AM
Harshit Chaudhary
Harshit Chaudhary - avatar
- 2
How to work Ai
10th Mar 2019, 5:05 AM
Sushant Chaudhary
Sushant Chaudhary - avatar