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

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

    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值