Here’s the problem. This code:
String a = "0000";
System.out.println(a);
char[] b = a.toCharArray();
System.out.println(b);
returns
0000 0000
But this code:
String a = "0000";
System.out.println("String a: " + a);
char[] b = a.toCharArray();
System.out.println("char[] b: " + b);
returns
String a: 0000 char[] b: [C@56e5b723
What in the world is going on? Seems there should be a simple enough solution, but I can’t seem to figure it out.
When you say
It results in a call to
print(char[] s)thenprintln()The JavaDoc for
print(char[] s)says:So it performs a byte-by-byte print out.
When you say
It results in a call to
print(String), and so what you’re actually doing is appending to aStringanObjectwhich invokestoString()on theObject— this, as with allObjectby default, and in the case of anArray, prints the value of the reference (the memory address).You could do:
Note that this is “wrong” in the sense that you’re not paying any mind to encoding and are using the system default. Learn about encoding sooner rather than later.