Home > Back-end >  Novice small white to ask, why rewrite equals method, print the result of the object toString, final
Novice small white to ask, why rewrite equals method, print the result of the object toString, final

Time:01-01

As shown in figure print one the value of the object,


Rewrite equals, print the object here, the output change


Please the great god answer is this why?
P.S. if there is new the two objects, attributes, same rewrite equals, print out the hash value of the same, what reason is this?

CodePudding user response:

In Java, hash code represents the characteristics of the object,
Such as object String str1="aa", str1. HashCode=3104
String str2="bb", str2 hashCode=3106
String str3="aa", str3 hashCode=3104
According to the HashCode which can be concluded that str1!=str2, str1==str3
Here are a few common hash code algorithm,
1, the Object class hashCode. [1] the memory address of the Object after processing, the structure, because each Object memory address is different, so the hash code are not the same as ,
2, the String class hashCode. According to the String class contains the content of the String, according to a special algorithm returns the hash code, as long as the heap space where the same String, the returned with the same hash code,
3, Integer class, returns the hash code is an Integer object contains the value of the Integer , such as Integer i1=new Integer (100), i1. HashCode value is 100, so, two Integer objects the same size, return to the same hash code,
  • Related