Java: println with char array gives gibberish
Asked Answered
G

6

28

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.

Goad answered 22/11, 2012 at 2:55 Comment(1)
You could simply do System.out.println(new String(b)); Using one of String's constructors to convert the char array into a String.Sexagenarian
B
31

When you say

System.out.println(b);

It results in a call to print(char[] s) then println()

The JavaDoc for print(char[] s) says:

Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

So it performs a byte-by-byte print out.

When you say

System.out.println("char[] b: " + b);

It results in a call to print(String), and so what you're actually doing is appending to a String an Object which invokes toString() on the Object -- this, as with all Object by default, and in the case of an Array, prints the value of the reference (the memory address).

You could do:

System.out.println("char[] b: " + new String(b));

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.

Berghoff answered 22/11, 2012 at 3:1 Comment(1)
I would start here: joelonsoftware.com/articles/Unicode.html - the gist of it is going to be that if you have a bunch of binary data, you really need to know the encoding in order to represent it textually, otherwise you're bound to have abnormalities.Berghoff
G
5

Use

System.out.println("char[] b: " + Arrays.toString(b));

The gibrish you get is the Class name followed by the memory address of the object. Problem occurs when you try to append b with a string char[] b: in this case the char array b.toString() method is called thus [C@56e5b723 is printed.

[ indicates that it is an array C indicates the class in this case char @56e5b723 indicates the memory location

Gloriane answered 22/11, 2012 at 2:58 Comment(1)
No, it's the hashCode() for the object. Here for a char array it just happens to be the internal address, but it's not guaranteed to be this for other objects especially if they override the hashCode() method.Sexagenarian
D
5
System.out.println("char[] b: " + b);

This is just like

System.out.println(("char[] b: " + b.toString());

You can look up "Object.toString()"

Dichroite answered 22/11, 2012 at 3:2 Comment(0)
P
4

An array's toString() method (which is what's called when you do "..." + b) is only meant to give debugging output. There isn't a special case where a char[]'s toString() will give you the original string - arrays of all types have the same toString() implementation.

If you want to get the original string from the char array, use:

String a2 = new String(b);
Pressey answered 22/11, 2012 at 3:2 Comment(0)
S
2

Use 3:e row!

Scanner input = new Scanner(System.in);
char[] txt = input.next().toCharArray();
System.out.println((char[])txt);
Squiggle answered 26/4, 2016 at 6:56 Comment(0)
H
0
private void print(char[] arr) {
    try {
        PrintStream stream
                = new PrintStream(System.out);
        stream.println(arr);
        stream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Hest answered 10/8, 2022 at 11:15 Comment(1)
Please explain what you want to achieve with this code and how it solves the problem/answers the question of the QP.Figured

© 2022 - 2024 — McMap. All rights reserved.