5

我正在玩char[]Java,并使用以下示例代码:

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

我得到以下输出:

[C@4367e003
Hello World !

我对此有一些疑问:

  1. 代表什么[C@4367e003?是内存地址吗?数字的含义是什么?是什么意思[C@

  2. 我一直认为调用println()一个对象会调用这个对象的 toString 方法,但似乎不是这样?

谢谢 !!

4

5 回答 5

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.

于 2013-09-04T23:04:55.287 回答
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.

于 2013-09-04T23:05:11.220 回答
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.

于 2013-09-04T23:05:04.460 回答
1

[C@4367e003 代表什么?是内存地址吗?数字的含义是什么?[C@ 是什么意思?

这是Object#toString字符数组的表示

  • [表示一个数组
  • C是一个编码字符,表示一个原始字符数组(所有编码在这里找到)

  • 4367e003是字符数组对象哈希码的十六进制表示

我一直认为在一个对象上调用 println() 会调用这个对象的 toString 方法,但似乎不是这样?

它确实会调用toString,但由于原始字符数组str不会覆盖 toString ,因此它调用Object#toString要查看数组的内容,您可以将数组包装在一个新的Stringusing 中new String(str)

于 2013-09-04T23:05:20.500 回答
0

第一个 println() 打印出字符数组的 toString() 方法。toString() 的默认实现是打印 a [、 a Cfor char、@符号,然后是数组的哈希码,默认情况下是数组的内存地址。

于 2013-09-04T23:08:42.830 回答