Using == to determine String in java is to determine the addresses of two objects instead of hashCode. Because you have two String objects, and because you use new for both of them, using == is false.
hashCode and == are different. == compares memory addresses, and hashCode is calculated based on instance variables.
You used new to create two String instances (the new keyword will open up new memory space), instead of directly referencing "hello" in the string pool. The following is the source code of this constructor.
== What is compared is the memory address. Hash and equals are almost obtained from each char of the string. Each char is the same and the hash is the same, but the memory address is different
== determines whether two reference variables point to the same object. When the contents of two objects are the same, their hashcodes are the same, but their references are not equal
I was not careful while reading and figured it out;
String
重写了hashCode
方法,其hashCode
The value is determined by the content, not the addressUsing == to determine String in java is to determine the addresses of two objects instead of hashCode. Because you have two String objects, and because you use new for both of them, using == is false.
hashCode and == are different. == compares memory addresses, and hashCode is calculated based on instance variables.
You used new to create two String instances (the new keyword will open up new memory space), instead of directly referencing "hello" in the string pool. The following is the source code of this constructor.
The hash calculation method of String is based on the value and hash in the above code.
== What is compared is the memory address. Hash and equals are almost obtained from each char of the string. Each char is the same and the hash is the same, but the memory address is different
== determines whether two reference variables point to the same object. When the contents of two objects are the same, their hashcodes are the same, but their references are not equal