+ 1

Can I know why this java code prints 'b'. Please help if you can.

char x = 'A'; char y = 'a'; char z = 'B'; z += y-x; System.out.println(z); o/p : b

3rd Aug 2019, 3:16 PM
Krishnanshu Dey
Krishnanshu Dey - avatar
5 Answers
+ 11
Krishnanshu Dey If you feel too much calculation in less time & need of remembering ASCII values in it, then it can be done without knowing ASCII value & no calculation: z='B' + 'a' - 'A' probably 'B'-'A' will be returning 1, so char z='a'+1 that will be 'b' happy coding
3rd Aug 2019, 3:34 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
Thanks all of you to sharing your knowledge with me. Now I can easily solve it.
3rd Aug 2019, 3:38 PM
Krishnanshu Dey
Krishnanshu Dey - avatar
+ 1
Thanks first to clear my doubt about this. So I have to remember those ASCII values. Is there any short trick to remember those values as I got this problem in challenge and it has only 30 sec to solve.
3rd Aug 2019, 3:31 PM
Krishnanshu Dey
Krishnanshu Dey - avatar