关于这个问题,查阅了网上的资料,发现证明过程太繁琐,这里我用了反证法。

java.lang.Object.hashCode()的返回值到底是不是对象内存地址?

hashCode契约

说到这个问题,大家的第一反应一定和我一样——去查Object.hashCode的源码,但翻开源码,看到的却是这样的(Oracle JDK 8):

 /**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code 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.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code 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.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code 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
* Java&trade; programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();

Object.hashCode()是一个native方法,看不到源码(Java代码,Oracle的JDK是看不到的,OpenJDK或其他开源JRE是可以找到对应的C/C++代码)。

上面这段注释指出了Object.hashCode()在JRE(JavaRuntime Library)中应该遵循的一些契约(contract): 
(PS:所谓契约当然是大家一致达成的,各个JVM厂商都会遵循)

(1)一致性(consistent),在程序的一次执行过程中,对同一个对象必须一致地返回同一个整数。

(2)如果两个对象通过equals(Object)比较,结果相等,那么对这两个对象分别调用hashCode方法应该产生相同的整数结果。(PS:这里equalshashCode说的都是Object类的)

(3)如果两个对象通过java.lang.Object.equals(java.lang.Ojbect)比较,结果不相等,不必保证对这两个对象分别调用hashCode()也返回两个不相同的整数

 因为每个对象的内存地址都是不一样的,如果hashCode()的返回值是对象内存地址,那么两个对象分别调用hashCode()方法将会返回两个截然不同的整数,这与契约三矛盾了,因此java.lang.Object.hashCode()的返回值不是对象内存地址!

在这里我觉得hashCode()方法的返回值是对象内存地址通过hash算法计算之后的哈希码值,因此才会出现两个对象分别调用hashCode()返回同一个值。

Java的Object.hashCode()的返回值到底是不是对象内存地址?的更多相关文章

  1. C++ 赋值构造函数的返回值到底有什么用?且返回值是否为引用类型有什么区别吗?

    首先定义类Person class Person{ public: string name; Person()=default; //默认构造函数 Person(string nam):name(na ...

  2. JAVA 框架 springmvc controller的返回值

    一.返回值:ModleView对象. 使用modelAndView.setViewName设置返回的页面.使用modelAndView.addObject设置返回的数据. @RequestMappin ...

  3. hashCode竟然不是根据对象内存地址生成的?还对内存泄漏与偏向锁有影响?

    起因 起因是群里的一位童鞋突然问了这么问题: 如果重写 equals 不重写 hashcode 会有什么影响? 这个问题从上午10:45 开始陆续讨论,到下午15:39 接近尾声 (忽略这形同虚设的马 ...

  4. python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样

    python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样

  5. java hashCode方法返回值

    hashCode 是和内存地址相关的一个整数. HashCode只是在需要用到哈希算法的数据结构中才有用 用途是为了方便快速地查找对象: HashMap 是根据键对象的 HashCode 来进行快速查 ...

  6. java matlab混合编程之返回值Struct类型

    java matlab混合编程的时候当返回值是Struct类型(matlab中的返回类型)如何来取得(java中)其值? 上网找,看到这个网页:http://www.mathworks.cn/cn/h ...

  7. Java中Object.hashCode contract

    面试时在这个问题上犯了个错误,只重写了equals方法,而没有覆盖hashCode()方法. 回来重读了Effective Java的Item 9,里面提到Object.hashCode contra ...

  8. Java多线程-新特性-有返回值的线程

    在Java5之前,线程是没有返回值的,常常为了“有”返回值,破费周折,而且代码很不好写.或者干脆绕过这道坎,走别的路了. 现在Java终于有可返回值的任务(也可以叫做线程)了. 可返回值的任务必须实现 ...

  9. JAVA多线程解惑之多线程返回值

    如果有人问题你,多线程可以有返回值吗?你怎么回答? 看下面例子,我定义了一个类实现了Callable 接口 public class MyCallable implements Callable< ...

随机推荐

  1. 【转】C# Async/Await 异步编程中的最佳做法

    Async/Await 异步编程中的最佳做法 Stephen Cleary 近日来,涌现了许多关于 Microsoft .NET Framework 4.5 中新增了对 async 和 await 支 ...

  2. static、extern分析

    1.extern extern在变量声明中有这样一个作用:你要在demo2.cpp中引用demo1.cpp中的一个全局变量,就要在demo2.h中用extern来声明这个全局变量(或者在demo1.h ...

  3. 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope

    解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...

  4. VC++使用CSocket发送HTTP Request时需要注意发送数据的编码格式

    VS2010以及更高版本中新建的MFC项目字符集默认是Unicode,CString创建的字符串默认是Unicode. 使用CSocket时,若以CString组织需要发送的HTTP Head时,那么 ...

  5. 扒一扒MathType不为人知的技巧

    MathType作为一款编辑数学公式的神器,很多人在使用它时只是很简单地使用了一些最基本的模板,很多功能都没有使用.MathType功能比你想象中的大很多,今天我们就来扒一扒MathType那些不为人 ...

  6. web.xml 中的listener、filter、servlet 加载顺序及其【配置详解】

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  7. vertica时间计算SQL语句实例:统计一天内登录的用户

    SQL语句实例: select count(id) as num from public.user where cast((CURRENT_TIMESTAMP-login_timed) day as ...

  8. Maven------使用maven新建web项目出现问题 项目名称出现红色交叉

    转载: http://wenda.so.com/q/1365963640069173?src=140 解决方法: problems窗口查看到下面错误java compiler level does n ...

  9. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  10. Mac下,如何把项目托管到Github上(Github Desktop的使用)

    在上一篇中,详细讲解了使用X-code和终端配合上传代码的方法,这种方法比较传统,中间会有坑,英文看起来也费劲,不过Github官方提供了一个Mac版的客户端,如下图: