Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java char[] toString

I'm playing arround with char[] in Java, and using the folowing sample code:

char[] str = "Hello World !".toCharArray();
System.out.println(str.toString());
System.out.println(str);

I get the following output:

[C@4367e003
Hello World !

And I have some questions about it:

  1. What does [C@4367e003 stands for? Is it a memory address? What's the meaning of the digits? What's the meaning of [C@?

  2. I always thought that calling println() on an object would call the toString method of this object but it doesn't seems to be the case?

like image 879
Nicolas C Avatar asked Sep 04 '13 23:09

Nicolas C


4 Answers

  1. [C means that it's a character array ([ means array; C means char), and @4367e003 means it's at the memory address[1]4367e003. If you want a string that represents that character array, try new String(str).

  2. println is overloaded; there is also a println that accepts a character array. If you don't pass a primitive, String, or char[], it will then call toString on the object since there's a separate overload for System.out.println(Object). Here is the documentation for the specific method that takes a character array.

    Here are the overloads for println (straight from the docs):

    void println()
    void println(boolean x)
    void println(char x)
    void println(char[] x)
    void println(double x)
    void println(float x)
    void println(int x)
    void println(long x)
    void println(Object x)
    void println(String x)
    

    You can read more about overloading at the bottom of this tutorial.


[1]: Technically, it's actually the hex representation of the object's hash code, but this is typically implemented by using the memory address. Note that it's not always really the memory address; sometimes that doesn't fit in an int. More info in this quesiton.

like image 176
tckmn Avatar answered Oct 19 '22 07:10

tckmn


It is calling toString() on the object. The object is an array that doesn't override the toString() method coming from Object. Object's implementation:

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

The [C means array ([) of char (C). The Javadocs for Class#getName() explain it in more detail.

The 4367e003 is the object's hash code, which is likely to be the memory address.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

When you don't call toString(), you are actually calling the println method that takes a char[], which knows how to print out the chars.

like image 25
rgettman Avatar answered Oct 19 '22 07:10

rgettman


It's the reference to your array that gets printed out as a string. What you want is Arrays.toString(str). And don't forget to import the Arrays class.

like image 1
blgt Avatar answered Oct 19 '22 07:10

blgt


What does [C@4367e003 stands for ? Is it a memory address ? What's the meaning of the digits ? What's the meaning of [C@ ?

This is the Object#toString representation of a character array

  • [ signifies an array
  • C is an encoding character indicating a primitive character array (all encodings found here)

  • 4367e003 is Hexadecimal depresentation of the character arrays Object hashcode

I always thought that calling println() on an object would call the toString method of this object but it doesn't seems to be the case ?

It does call toString but since the primitive character array str does not override toString to it calls Object#toString To see the contents of the array you can wrap the array in a new String using new String(str).

like image 1
Reimeus Avatar answered Oct 19 '22 07:10

Reimeus