A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李峥 中级黑马   /  2015-7-20 20:25  /  445 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一直有一个误区,以为hashCode获取的值是对象的地址值:
                String s0="helloworld";
                String s1=new String("helloworld");
                String s2="hello" + new String("world");
                System.out.println( s0==s1 ); //false  
                System.out.println( s0==s2 ); //false
                System.out.println( s1==s2 ); //false
                System.out.println(s0.hashCode());//-1524582912
                System.out.println(s1.hashCode());//-1524582912
                System.out.println(s2.hashCode());//-1524582912


结果是哈希值一样,但地址值却不一样,也就是说,哈希值不是地址值.
通过API,String 类中哈希值计算方式如下,字符串一样,哈希值是一样的.
【返回此字符串的哈希码。String 对象的哈希码根据以下公式计算:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
使用 int 算法,这里 s 是字符串的第 i 个字符,n 是字符串的长度,^ 表示求幂。(空字符串的哈希值为 0。) 】

那么,请问如何获取对象的地址值?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马