hashCode()方法 和 hash()方法

hashCode()方法 和 hash()方法
    String str = "abc";
String str1 = "abc";
System.out.println(str == str1); //true 从常量的缓冲池里面拿出来
System.out.println(judge("")); //true
System.out.println( judge(new String("")) );//false 牛客给的会不会是这个
System.out.println( new String("").equals("") );//true }
public static boolean judge(String str){
if(str == "")
return true;
else
return false;
}
凡是代码中出现"abddf" 双引号这些的 都是常量池 ,直接拿 == 比较
hashCode在JAVA中,代表了对象的一种特征。比如任何对象都有toString方法一样
只要比较2个的哈希码(是一个int数字)不同则这肯定是不同的对象。反过来不一定
同一个对象有相同的hashcode值  逆否:hashcode不同,对象不同, 若hashcode同了,然后再判断内容
哈希码是一种数据结构算法,在JAVA中,常见的哈希码算法获取到的哈希码具体值是怎么获取到的呢?

  相同对象放到的是一个对象池子里面, 里面的取出来的对象的hashcode肯定是同 
String a ="abcd";
String b = "abcd";
System.out.println(a.hashCode() +" " + b.hashCode());
  2987074  2987074

1.在String类中的hashCode是根据String类中包含的字符串获取的,根据哈希算法获取到一个哈希码,那么只要我的字符内容是相等的,我的哈希码也是相同的。


        Integer a =11;
Integer b = 11;
System.out.println(b.hashCode()==a.hashCode()); //true

Integer a =12345;
System.out.println(a.hashCode()); // 输出 12345

2.Integer类中的hashCode和String是一样的,也是根据类中包含的值去生成的哈希码。两个相同大小的integer值,那么它的hashCode也是相等的


3.Object类中的hashCode则和之前的不一样了,他是根据对象的内存地址经过哈希算法之后获取到的哈希码,由于每个对象的内存地址不相同,所以hashCode是一般不同的。

hashCode()方法 和 hash()方法


上一篇:jquery实现很简单的DIV拖动


下一篇:java httpclient cookie