关于Object中的hashCode方法
来源:互联网 发布:淘宝美工好找工作吗 编辑:程序博客网 时间:2024/06/07 23:01
通过jdk源码可看到public native int hashCode();
此方法是交给C++等实现的,在Oracle的jdk中看不到此实现方法,但是在OpenJDK中可看到,如下
// hashCode() generation ://// Possibilities:// * MD5Digest of {obj,stwRandom}// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.// * A DES- or AES-style SBox[] mechanism// * One of the Phi-based schemes, such as:// 2654435761 = 2^32 * Phi (golden ratio)// HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;// * A variation of Marsaglia's shift-xor RNG scheme.// * (obj ^ stwRandom) is appealing, but can result// in undesirable regularity in the hashCode values of adjacent objects// (objects allocated back-to-back, in particular). This could potentially// result in hashtable collisions and reduced hashtable efficiency.// There are simple ways to "diffuse" the middle address bits over the// generated hashCode values://static inline intptr_t get_next_hash(Thread * Self, oop obj) { intptr_t value = 0 ; if (hashCode == 0) { // This form uses an unguarded global Park-Miller RNG, // so it's possible for two threads to race and generate the same RNG. // On MP system we'll have lots of RW access to a global, so the // mechanism induces lots of coherency traffic. value = os::random() ; // 随机数 } else if (hashCode == 1) { // This variation has the property of being stable (idempotent) // between STW operations. This can be useful in some of the 1-0 // synchronization schemes. // 地址基础上hack intptr_t addrBits = intptr_t(obj) >> 3 ; value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ; } else if (hashCode == 2) { value = 1 ; // for sensitivity testing, 实际不会使用 } else if (hashCode == 3) { value = ++GVars.hcSequence ; } else if (hashCode == 4) { value = intptr_t(obj) ; // 直接用地址 } else { // Marsaglia's xor-shift scheme with thread-specific state // This is probably the best overall implementation -- we'll // likely make this the default in future releases. unsigned t = Self->_hashStateX ; t ^= (t << 11) ; Self->_hashStateX = Self->_hashStateY ; Self->_hashStateY = Self->_hashStateZ ; Self->_hashStateZ = Self->_hashStateW ; unsigned v = Self->_hashStateW ; v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ; Self->_hashStateW = v ; value = v ; } value &= markOopDesc::hash_mask; if (value == 0) value = 0xBAD ; assert (value != markOopDesc::no_hash, "invariant") ; TEVENT (hashCode: GENERATE) ; return value;}
可看到获得hash值得方法有5种,其中包括随机数、地址基础上进行处理、默认值1、直接地址、一个称为Marsaglia的方法,其中默认的就是第5种,即Marsaglia的方法。
参考:Java的Object.hashCode()的返回值到底是不是对象内存地址?
另外String的hashCode被重写。是根据String值生成的。
阅读全文
0 0
- 关于Object中的hashCode方法
- Object中的hashCode()方法
- 重写Object中的hashCode方法
- 重写object中的hashcode和equals方法
- object类中的equals和hashcode方法
- 关于java中的hashCode()方法
- 关于java中的hashCode方法
- 关于Object类的equals方法和hashCode方法
- Java中的hashCode()方法与equals(Object)方法
- Java Object.hashCode()方法
- Java Object.hashCode()方法
- 关于Java中的hashCode()和equals(Object other)
- 关于Object类的hashcode()方法的特点
- 关于Object类的hashcode()方法的特点
- 如何覆盖Object类中的equals(),hashCode(),toString()方法
- Object类中的toString(),equals(), hashCode()方法之间的关系
- Object下的hashCode方法
- java.lang.Object.hashCode()方法
- 提示msvcr71.dll丢失处理办法
- layui 阻止图片上传(before 方法)
- Android 悬浮弹窗window dialog
- Hibernate单独使用 (一)建立和基本使用方法
- oracle 查看表操作历史记录
- 关于Object中的hashCode方法
- c#中如何实现拷贝对象
- spring spring mvc hibernate整合遇到的异常
- 模式识别与机器学习(第一章)学习笔记
- RecyclerView的简单使用(一)———— 实现各种布局效果
- springcloud《一》--认识spring boot
- TFTP服务器的使用
- Android动画分类
- maven_如何导包