+ 2

Explain pls the output of this code

public static void main(String[] args){ char[] test = {'a', 'b', 'j', 'l', 'm'}; System.out.println((char)((--test[4])-1)); The output is k

17th Apr 2018, 8:25 AM
Ä°skender GĂŒneƟ
Ä°skender GĂŒneƟ - avatar
5 Answers
+ 3
first it gets test[4] --> m then you need to understand that char is just an encoded number in ASCII http://www.torsten-horn.de/techdocs/ascii.htm I used that in my code to tell what letters there are: https://code.sololearn.com/cRL3AllAR3N9/# --test[4] --> the letter 'm' in ASCII is 109. Decreased to 108 and written back into the char array because of '--', the array becomes {'a', 'b', 'j', 'l', 'l'} and the letter for further calculation is the second 'l' 'l' - 1 --> 108 - 1 --> 107 (char) 107 --> 'k' . nonzyro no offense but this proves you are wrong with your 'decreased the pointer, pointing it at memory containing 'l'': https://code.sololearn.com/cXbc3Hj0o2sP/#java
17th Apr 2018, 8:51 AM
Johann Leis
Johann Leis - avatar
+ 2
Johann Leis Sorry, I just reread the whole thread. :( I must've misread the question (when I saw "(char)" I saw "(char*)" and assumed it was a cast and pointer arithmetic, I didn't even notice it was a Java question) I admit I only skimmed your code and probably misread that too. I have no idea if you've edited it since, but there's nothing wrong with it whatsoever. I remember thinking, "Why's he using Java?" Anyway, since my answer's on top and very misleading, I'll delete it. I'll delete my other post too, since your code was isn't wrong.
23rd Apr 2018, 11:17 AM
non
+ 2
nonzyro no problem; proves you're human😉 i'll delete my complains too
24th Apr 2018, 8:18 AM
Johann Leis
Johann Leis - avatar
+ 1
Johann Leis nonzyro Donna thankyou guys! that was helpful â˜ș
25th Apr 2018, 3:41 PM
Ä°skender GĂŒneƟ
Ä°skender GĂŒneƟ - avatar