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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 927233671 中级黑马   /  2014-6-22 19:59  /  1336 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 927233671 于 2014-6-23 18:27 编辑
  1. class  Demo1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String s1=new String("zhangsan");
  6.                 String s2=new String("zhangsan");
  7.                 System.out.println(s1==s2);
  8.                 System.out.println(s1.equals(s2));
  9.                 System.out.println((s1.hashCode())==(s2.hashCode()));//为什么s1和s2的哈希值是一样的
  10.                
  11.                


  12.         }
  13. }
复制代码

4 个回复

倒序浏览
两个对象都是引用了字符串池中的“zhangsan”,因此其hashcode是相等的。   
回复 使用道具 举报
因为字符串内容完全一样啊!
回复 使用道具 举报
- -因为String类覆写了 objcet 类的hashcode方法……如果是其他类,比如你自己创建的,如果你不覆写hashcode方法,那就不等了,我是菜鸟,理解只能到这一步
回复 使用道具 举报
请见API里对hashcode()的说明
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马