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:
What does [C@4367e003
stands for? Is it a memory address? What's the meaning of the digits? What's the meaning of [C@
?
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?
[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)
.
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.
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.
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.
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 arrayC
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With