Java源码解析:hashCode与相同对象的关系

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_37107022/article/details/75946383

1.普通类对象

1. hashCode相同,不一定是同一个对象
2. 同一个对象的,hashCode值一定相同

这里写图片描述

2. 数值型的原始数据类型对应的包装类

只要值是一样的,hashCode就会是相同的。尽管不同的数值类型的包装类,计算hashCode的算法不一样,但是底层都是拿对应的原始数据类型的值去进行hashCode计算。

以Double类为例

这里写图片描述


3. 测试代码如下

/**
 *hashCode相同,不一定是同一个对象
 *同一个对象的,hashCode值一定相同 
 *
 *-------------------------------------------------------------------------------
 *普通对象的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. 
 *-------------------------------------------------------------------------------
 *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. 
 *-------------------------------------------------------------------------------
 *
 */
public class Test {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Test() {
        super();
    }

    public Test(String name) {
        super();
        this.name = name;
    }

    public static void main(String[] args) {
        System.out.println("--------------------普通对象-----------------------");

        Test test3=new Test();
        Test test4=new Test();
        System.out.println(test3.hashCode());//2018699554
        System.out.println(test4.hashCode());//1311053135

        Test test=new Test("我");
        Test test2=new Test("我");
        System.out.println(test.hashCode());//366712642
        System.out.println(test2.hashCode());//1829164700

        System.out.println("--------------------String-----------------------");

        String s1="abc";
        String s2="abc";
        System.out.println(s1.hashCode());//96354
        System.out.println(s2.hashCode());//96354

        String s5=new String("abc");
        String s6=new String("abc");
        System.out.println(s5.hashCode());//96354
        System.out.println(s6.hashCode());//96354

        String s3=new String();
        String s4=new String();
        System.out.println(s3.hashCode());//0
        System.out.println(s4.hashCode());//0

        /**
         * 数值型原始类型对应的包装类(Byte,Short,Integer,Float,Double),hashCode算法都是基于
         * 对应的原始数据类型,所以只要包装类的数值相同,那么hashCode必然相同
         * 
         * Double类关于hashCode源码说明:
         * Double类的hashCode是根据对应的double值计算获得的。
         * 
         * Returns a hash code for a {@code double} value; compatible with
         * {@code Double.hashCode()}.
         *
         * @param value the value to hash
         * @return a hash code value for a {@code double} value.
         * @since 1.8
         */
        /*Double类关于hashCode源码:
         * public static int hashCode(double value) {
            long bits = doubleToLongBits(value);
            return (int)(bits ^ (bits >>> 32));
        }*/
        System.out.println("--------------------原始类型对应的包装类-----------------------");
        Double d1=5.0;
        Double d2=5.0;
        System.out.println(d1.hashCode());//1075052544
        System.out.println(d2.hashCode());//1075052544

        Double d3=new Double(5.0);
        Double d4=new Double(5.0);
        System.out.println(d3.hashCode());//1075052544
        System.out.println(d4.hashCode());//1075052544

    }
}
展开阅读全文

没有更多推荐了,返回首页