I encountered an assignment question giving me
String test = "problemnumber3";
System.out.println(test.charAt(1));
System.out.println(test.charAt(7));
String piece = test.charAt(13)+4+"7"+test.charAt(13);
System.out.println(piece);
and asking what is printed. The answer I came up with looking at the code was that it would print
r
n
773
but decided to enter it into BlueJ (what we use in class) to double check. The code printed out
r
n
5573
and I can’t figure out where the 55 is coming from. I’ve checked all the available notes and lecture material I can.
Do the numeric characters from strings add to constants weirdly or what?
It’s the ASCII representation of the character
'3', (automatic conversion) with4added to it.48is'0',49is'1', so51is'3'. Then the compiler adds4to get55, then Java’sStringautomatically converts the55to aStringrepresentation of55when you do+ "7".