class A { } class Test extends A {    public static void main( String[] args) { 
    A obj=new Test() ; 
   
    System.out.println(obj); 
     
   } 

输出为: Test@de6cde ****************************************************************************************大概意思是,方法toString返回一个包含对象本身所属类的名字加上字符@ 
以及这个对象的哈希码(为十六进制形式)的字符串 
toString 
public String toString() 
Returns a string representation of the object.  
In general, the toString method returns a string that "textually represents" this object. 
The result should be a concise but informative representation that is easy for a person to read. 
It is recommended that all subclasses override this method.  
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. In other words,  
this method returns a string equal to the value of:    
getClass().getName() + '@' + Integer.toHexString(hashCode()) 
  Returns: 
a string representation of the object.那么这个结果的物理意义是什么呢? 

解决方案 »

  1.   

    原始 Object 的 hashCode 是本地方法获得的内存地址。@ 前面的是类的名字。数组以 [ 开头,每个 [ 表示一维数组,对象类型数组,比如:String[] 表示为 [Ljava.lang.String; 其中“L”表示引用类型。数组类型表示为:[B —— byte 一维数组
    [C —— char 一维数组
    [D —— double 一维数组
    [F —— float 一维数组
    [I —— int 一维数组
    [J —— long  一维数组
    [L<classname>; —— 对象类型一维数组
    [S —— short 一维数组
    [Z —— boolean 一维数组比如:[[[C@xxxxxx 表示“三维的 char 数组”。具体的可以看看 The Java Virtual Machine Specification
      

  2.   

    记得原来学C++的时候还研究过类似的问题,呵呵,不过到Java了,要以JVM规范为准。楼主想知道的是hashCode值的物理意义是什么对吧?
    其实这个值跟内存地址没有关系,这只是JVM内部用来管理对象用的一个唯一标识,两个对象的hashCode值相等标识JVM对它们的标识是相同的,并不一定代表内存地址就一致或者并不一定代表它们就是引用的同一个对象。我们都知道"=="在Java中是用来比较内存地址的运算,hashCode到底会怎么计算出来呢,是否是内存地址呢,看一个简单的例子就可以证明了:
    String str1 = "hello";
    String str2 = "hello";
    String str3 = new String("hello");System.out.println(str1==str2); //print true
    System.out.println(str1==str3); //print false
    System.out.println(str1.hashCode()==str2.hashCode()); //print true
    System.out.println(str1.hashCode()==str3.hashCode()); //print true
    Java API Document关于hashCode方法的一句话说得很清楚: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.就是说这个hashCode值通常来说是用这个对象的内存地址来计算出来的,但实现技术并不受限制,原理就像当初我学C++去取唯一随机数可以选择用系统时间做种子去计算一样。但由于它只是一个标识并且可以被重写,你想return多少都可以,所以不要误解了它就是内存地址。在Java中它唯一的作用是被一些Collection来标识是否包含重复元素用的。
      

  3.   

    火龙果兄你也说错了一回哦,呵呵!看我的例子里面str1和str3明明就不是同一块地址,但hashCode值却是相同的哦!所以hashCode不是内存地址啊,从来也没有这种说法!
      

  4.   


    呵呵,String 的 hashCode 方法被改写过的,采用每个字符进行计算的,只要两个字符串的内容相同,
    那么它们的 hashCode 肯定是相同的。
      

  5.   

    在没有被重写过 hashCode 方法的类中,其 hashCode 返回的是内部地址,即内存地址。
      

  6.   

    呵呵,我在 3 楼的第一句加了限定条件的原始 Object 的 hashCode 是本地方法获得的内存地址。 
      

  7.   

    唉,还是火龙果你功力深厚,呵呵!
    其实追溯到native就没有定论了,虚拟机到底怎么计算它我还真不确定,拿内存地址当然也是最常见的做法之一
    向你致敬!
      

  8.   

    String s1 = "hello,world";
    String s2 = new String("hello,world");
    这两者是有区别的,前者是在java对象池中取,后者是在堆中分配新空间。
    所以
    String s1 = "hello,world";
    String s3 = "hello,world";s1 == s3 返回true;而
    String s2 = new String("hello,world");
    String s4 = new String("hello,world");s2 == s4 返回false;另:
       对于数值类型,比如Integer, Java对象池只支持-128~127只见的数
    例:
       Integer i1 = 1;
       Integer i2 = 1;
       i1 == i2 返回true;   Integer i3 = 287;
       Integer i4 = 287;
       i3 == i4 返回false;