java语言hashcode和真实物理地址的关系以及相关几个问题

背景:Object o1 = new Object();现在有一个已经实例化的o2,我想知道o2是否与o1是同一个对象。
问题1.System.out.println(o1);输出的是什么?是类似classname@hashcode吗?
问题2.Object里面的equals方法中的if(o1==o2){return true;},其中“==”比较的是什么?是hashcode还是物理地址?如果是物理地址,是不是可以说明System.out.println(o1);和if(o1==o2){return true;}中的o1不是一回事?
问题3.如何让java打印某个对象的物理地址?

第一,输出的是:getClass().getName() + "@" + Integer.toHexString(hashCode()),这是jdk对toString方法的实现。

第二,“==”比较的就是一个对象的引用地址是否相同,即是不是一个对象。即不是左右两边非一个对象的话,该运算就返回false。

hashcode和物理地址没关系,只是一种约定,并非强制性的。主要是为相关哈希集合及运算服务的。再具体的部分要看jdk关于hashcode的解释了。

第三,java没有指针操作,但到处都是指针操作,即对象操作,即通过对象操作指针即物理地址,而本身是不可以看到物理地址,也就不能直接操作了,全都交给jvm了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-17
这个问题的解释其实很复杂,就简单其鉴来说,直接回答楼主的3个问题:
1、输出的是:getClass().getName() + "@" + Integer.toHexString(hashCode()),这是jdk对toString方法的实现。
2、“==”比较的就是一个对象的引用地址是否相同,即是不是一个对象。即不是左右两边非一个对象的话,该运算就返回false。
hashcode和物理地址没关系,只是一种约定,并非强制性的。主要是为相关哈希集合及运算服务的。再具体的部分要看jdk关于hashcode的解释了。
3、java没有指针操作,但到处都是指针操作,即对象操作,即通过对象操作指针即物理地址,而本身是不可以看到物理地址,也就不能直接操作了,全都交给jvm了。
再思考下吧。本回答被提问者和网友采纳
第2个回答  2013-08-17
问题1,对的Object o1=new Object();
System.out.println(o1); 输出java.lang.Object@12d96f2
System.out.println(o1.hashCode());输出19764978,其实就是12d96f2的十进制形式
问题二,"=="比较的o1和o2指向堆栈的地址,因为o1和o2都是对象。对象不同于常量,它在堆栈中声明一个地址,这个地址指向对象在堆内存中的实际内存。
问题三,这个没研究过。

ps:

Object o1 = new Object();

Object o2 = new Object();
因为o1在堆栈中是一个地址,o2在堆栈是另一个地址,所以"=="是用于不可能相等的。
所以有的时候,我们把equals方法重载,根据对象里的属性来判断两个对象是否相等。
实际上除非你写成这样。
Object o1 = new Object();
o2=o1;
"=="才会判断相等,因为这时候两个对象的堆内存指向堆栈的同一个地址。
第3个回答  2013-08-23
//这是System.out.println的实现,把Object变成string打印出来
public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }
//那String.valueOf干了什么?
public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
//接下来就是toString了
public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
//hashCode怎么生成?
public native int hashCode();
//好,悲剧了。不过有人留了段注释交代hashcode应该是个什么样子的
/*
The general contract of hashCode is:
只要程序没死,某个object的hashcode不能变,除非equals方法里用到的条件变了。

当然程序挂掉重启的话可以变
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.
两个object equal的话,hashcode一定相同
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects 
must produce the same integer result.
两个object不equal的话,hashcode倒也不一定不同
不过用在hash table的时候,不同object保证有不同hashcode会提高性能的哦
It is not required that if two objects are unequal according to the 
java.lang.Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects 
must produce distinct integer results. 
However, the programmer should be aware 
that producing distinct integer results for unequal objects may improve the 
performance of hash tables.
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.)
*/
//虽说Object的hashCode时常和物理地址有关,但难保有些非主流虚拟机不按常理出牌……
//所以看jdk的代码比问百度知道要清楚多了

下面两个问题erliang20088已经专业回答了