黑马程序员技术交流社区

标题: 如何获取对象的地址值? [打印本页]

作者: 李峥    时间: 2015-7-20 20:25
标题: 如何获取对象的地址值?
一直有一个误区,以为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。) 】

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





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2