为什么stringObject
的hashcode是我提供的字符串?
String s = new String(); // here the hascode is 0.
但是当我获得我创建的某个对象的哈希码时。
testClass o = new testClass(); // the hashcode is some random number.
我的问题是两者有什么区别?我的理解是 hashCode()
来自 Object
类,它是所有类的母亲。
最佳答案
Why is the hashcode of stringObject is the string I provided? ex. String s = new String("hello"); // here the hascode is hello.
这不是真的
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`
关于java - 为什么 String 对象的 hashCode() 与自定义类对象的 hashCode() 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945329/