1

I'm moving from C to Java now and I was following some tutorials regarding Strings. At one point in the tutorials they showed instantiating a new string from a character array then printing the string. I was following along, but I wanted to print both the character array and the string so I tried this:

class Whatever {
    public static void main(String args[]) {
        char[] hello = { 'h', 'e', 'l', 'l', 'o', '.'};
        String hello_str = new String(hello);
        System.out.println(hello + " " + hello_str);
    }
}

My output was something like this:

[C@9304b1 hello.

Clearly, this is not how you would print a character array in Java. However I'm wondering if I just got garbage? I read on some site that printing a character array give you an address, but that doesn't look like an address to me... I haven't found a lot online about it.

So, what did I just print?
and bonus questions: How do you correctly print a character array in java?

Mike
  • 47,263
  • 29
  • 113
  • 177
  • possible duplicate of [string to char array, showing silly characters](http://stackoverflow.com/questions/13641462/string-to-char-array-showing-silly-characters) – jlordo Dec 07 '12 at 14:26

1 Answers1

8

However I'm wondering if I just got garbage?

No, you got the result of Object.toString(), which isn't overridden in arrays:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

So it's not garbage, in that it has a meaning... but it's not a particularly useful value, either.

And your bonus question...

How do you correctly print a character array in java?

Call Arrays.toString(char[]) to convert it to a string... or just

System.out.println(hello);

which will call println(char[]) instead, which converts it into a string. Note that Arrays.toString will build a string which is obviously an array of characters, whereas System.out.println(hello) is broadly equivalent to System.out.println(new String(hello))

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @JonSkeet.. Surprisingly, if we print the character array before creating a string out of it, we get the `hello.` as result. Why a different behaviour in printing before the creation of string, and after that? – Rohit Jain Dec 07 '12 at 14:12
  • @RohitJain: That sounds unlikely to me. I suspect you're misdiagnosing it. – Jon Skeet Dec 07 '12 at 14:13
  • @JonSkeet.. `char[] hello = { 'h', 'e', 'l', 'l', 'o', '.'}; System.out.println(hello); String hello_str = new String(hello); System.out.println(hello + " " + hello_str);` This code is working strangely. :( – Rohit Jain Dec 07 '12 at 14:14
  • 4
    @RohitJain: That's calling `println(char[])` rather than `println(Object)`, so it's not calling `toString()`. Try changing it to `"" + hello` (for example) and you'll see the original output. – Jon Skeet Dec 07 '12 at 14:15
  • 1
    @JonSkeet perhaps you should mention `println(char[])` in the answer. – John Dvorak Dec 07 '12 at 14:18
  • Neat answer. I thought it was hex, but the `@` and the `[` were throwing me. So a character array is of class `[C`? That's a weird class name... – Mike Dec 07 '12 at 14:20
  • @Mike: `[` indicates an array, and `C` is the short name for the `char` primitive type. (See the docs for `Class.getName()` for details.) – Jon Skeet Dec 07 '12 at 14:21