为什么哈希码stringObject是我提供的字符串?
String s = new String(); // here the hascode is 0.
Run Code Online (Sandbox Code Playgroud)
但是当我得到我创建的某个对象的哈希码时.
testClass o = new testClass(); // the hashcode is some random number.
Run Code Online (Sandbox Code Playgroud)
我的问题是两者有什么区别?我的理解是hashCode()来自Object班级,是所有阶级的母亲.
为什么stringObject的哈希码是我提供的字符串?恩.String s = new String("hello"); //这里的hascode是你好的.
那不是真的
hashCode() 是一个int数
您似乎将hashcode()与toString()混淆
如果你想检查一下 hashcode()
String str = "hello";
System.out.println(str.hashcode());//will print some int number
System.out.println(str);//will invoke toString() which will return `hello`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |