+ 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?
16 Respuestas
+ 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
+ 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'.
+ 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')
+ 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
+ 7
RISHABH MISHRA Please don’t copy other people’s answers. Please remove your copy.
+ 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
+ 6
Thanks everyone
Now from where can I get ascii codes for all the keys and symbols of keyboard.
+ 4
You need to be familiar with data types and Ascii codes
+ 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
+ 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
+ 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.
+ 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
+ 1
Its ans is b
- 2
How to work Ai