String [] test = {"a", "b", "c"};
System.out.println(test);执行上面两行代码可得到一个输出结果如下:
[Ljava.lang.String;@12498b5请问:
这个输出结果的含义是什么?

解决方案 »

  1.   

    1. String[] 也是一个 Object;2. 根据 Sun 提供的 JDK 源代码,Object.toString() 的实现是下面这个样子:    public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }3. 根据 Sun 提供的 JDK 源代码,Object.hashCode() 是下面这个样子:    public native int hashCode();4. 至于 native 的 hashCode() 内部怎么实现,也能从 JDK 的源代码里找到,我没仔细看,但恐怕不只是“内存地址”这么简单;
      

  2.   

    你这样的两句代码输出的是字符串数组test的地址。如果你想输出数组内容,你最好重写toString()方法。
      

  3.   

    难道内存地址是用hash code表示的?
      

  4.   

    按maquan的说法,@后面只是一个依据本地实现的hash码,为什么前边很多说是内存地址呢?依照我的判断,@后面的不是内存地址,而maquan已经从源代码的角度给了很好的解释。不知道对否?请明判!
      

  5.   

    我查了元代码,有如下描述:对toString():
    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.对hashCode():
    Returns a hash code value for the object. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. 
    关于hashCode方法,更详细的描述可以参考http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html由此可见,@后面的hash code不是什么内存地址,而是标示对象的一个int值.!!!!native的hashCode()的代码在哪里?
      

  6.   

    jacshan(猎人) 是对的,@后面是散列码
      

  7.   

    hashCode是用来唯一标识对象,在此数组初始化时回为其制定,虽然不是内存地址,但实际上Object.HashCode()的返回值与内存地址是相关联的,当然这个方法可被覆盖,但是不重写的化,调用的是基类的方法,所以可以理解为内存地址
      

  8.   

    yanxi202122说得有道理,@后边的hash code可以理解成内存地址,虽然它不是。native的hashCode()的代码在哪里?可以通过它找到这个hash code和内存地址之间的联系么?
      

  9.   

    TO caoi(衣水的人) :如果你真的想了解,如果你手里有 JDK 1.4.2_01 的源代码,那么,可以按照下面的顺序去看看:
    (我没有仔细看到最后,太累了,但已经可以肯定,hashCode 并不是简单的取内存地址)<JDK142_01-src>\j2se\src\share\native\java\lang\Object.c
        static JNINativeMethod methods[]<JDK142_01-src>\hotspot\src\share\vm\prims\jvm.cpp
        JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))<JDK142_01-src>\hotspot\src\share\vm\oops\oop.inline.hpp
        inline intptr_t oopDesc::identity_hash()<JDK142_01-src>\hotspot\src\share\vm\oops\oop.cpp
        intptr_t oopDesc::slow_identity_hash()<JDK142_01-src>\hotspot\src\share\vm\runtime\synchronizer.cpp
        intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj)    ……
      

  10.   

    谢谢maquan('ma:kju),好奇怪,为什么1.5的JDK附的src没有那几个C/CPP文件?
      

  11.   

    [Ljava.lang.String 看到[了么?这个是集合元素 尤其是数组的标志
      

  12.   

    JDK 附带的 src.zip 只包含了部分 JAVA 程序的源代码,全部的源代码需要单独下载。http://www.sun.com/software/communitysource/j2se/java2/download.xml