为什么它为两个字符串对象生成相同的哈希码? [英] Why does it generate same hash code for both string objects?
本文介绍了为什么它为两个字符串对象生成相同的哈希码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java编程的新手,我想创建多个字符串,为此,我编写了以下代码.为什么两个字符串对象都生成相同的哈希码?
I am new to Java programming, I want to create are multiple numbers of strings, for that I written the code below. Why does it generate same hash code for both string objects?
class SingletonClass {
public static String mystring="this";
}
public class SingletonObjectDemo {
public static void main(String args[]) {
String str = SingletonClass.mystring;
String str2 = SingletonClass.mystring;
System.out.println("String str "+str.hashCode());
System.out.println("String str1 "+str2.hashCode());
}
}
推荐答案
它正在为两个字符串对象生成相同的哈希码...因为它们是同一对象.
It's generating the same hash code for both string objects...because they're the same object.
这篇关于为什么它为两个字符串对象生成相同的哈希码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文