5

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nicolas C
  • 1,584
  • 2
  • 17
  • 33
  • new String(chararray); creates a string from a char array, and yes thats a memory address i think the C shows its a char array and the at literally means at the address is the location in memory where the array is held. – tom Sep 04 '13 at 23:04
  • Since arrays are classes, they extend from `Object` and do not override `toString` method, so they will use [`Object#toString`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29) (read the link to understand the result in `System.out.println(array)`). – Luiggi Mendoza Sep 04 '13 at 23:05
  • http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29 – Alfonso Nishikawa Sep 04 '13 at 23:06
  • possible duplicate of [Why does the toString method in java not seem to work?](http://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work) – Daniel Kaplan Sep 04 '13 at 23:06
  • 1
    @tieTYT Not a duplicate. This one ask's why `System.out.println(str);` prints the value in the array, not the value of `str.toString();`. – Justin Sep 04 '13 at 23:11
  • @gangqinlaohu Yes, but if you look at the accepted answer, it answers his questions. – Daniel Kaplan Sep 04 '13 at 23:16
  • Possible duplicate of [What is the meaning of the information that i get by printing an Object in Java?](http://stackoverflow.com/questions/4696103/what-is-the-meaning-of-the-information-that-i-get-by-printing-an-object-in-java) – Raedwald Jan 24 '16 at 17:57

5 Answers5

8
  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.

Community
  • 1
  • 1
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • It's a typical mistake to believe that the number in the default toString representation is an address. In fact, it is the hexadecimal representation of the object's hash code. – jarnbjo Sep 04 '13 at 23:15
3

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

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.

blgt
  • 8,135
  • 1
  • 25
  • 28
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@ ?

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).

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

The first println() is printing out the toString() method of a character array. The default implementation of toString() is printing a [, a C for char, the @ symbol, and then the hash code of the array which is by default the array's memory address.

MasterOfBinary
  • 245
  • 1
  • 12