a comparison of the content. Of course, the base type is a comparison of the values.It should be noted that the Java language requirements for Equals () are as follows, and these requirements must be followed:1. Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".2. Reflectivity: X.equals (x) must return is "true".3. Analogy: If X.equals (y) returns "true" and Y.equals (z)
Case:For example, a person at different times in the system generated two instances, to determine whether these two instances is a person, compare the ID number on it. Assuming these two instances, one is a file created at age 16, one is a 24-year-old file, and if you do not rewrite the Equals method, these two instances are definitely not a person.What if the hashcode and
equals () method to compare the content. Of course, the basic type is to compare values. There is nothing to say about it.We should also note that the requirements of the Java language for equals () are as follows, which must be followed:? Symmetry: If X. Equals (y) returns "true", then Y.
java = =, Equals (), hashcode () Source Code AnalysisIn Java programming or interview often encounter = =, equals () comparison. I looked at the source code, the actual programming summary.1. = == = In Java is the address that com
general contract of the Hashcode method, where the object that declares equality must have an equal hash codePanax Notoginseng System.out.println (P1.hashcode ()); - System.out.println (P2.hashcode ()); the } +}Person Class Code:1 Packagestudy;2 3 ImportJavax.print.attribute.HashAttributeSet;4 5 Public classPerso
classes are also suitable for this principle. Of course, the class that has not been overwritten will also follow this principle after it inherits the equals () and hashcode () Methods of the object class.
4. When talking about hashcode () and equals (), we can't help but talk about the usage of hashset, hashmap, and
. And so on, you can know that the overridden equals () and Hashcode () methods in a wrapper class such as Integer and double are also appropriate for this principle. Of course, there are no overridden classes that follow this principle after inheriting the Equals () and Hashcode () methods of the object class.4. When
and double encapsulation classes are also suitable for this principle. Of course, it has not been rewritten.Class. This principle is also observed after the equals () and hashcode () Methods of the object class are inherited.
4. When talking about hashcode () and equals (), we can't help but talk about the usage of ha
Summary:This paper mainly records my hashcode and equals two knowledge points of the learning Process.From the time of students to learn java, know hashcode and equals the two methods, the use of the Equals method is particularly
. Wait, these classes are all overriding the Equals () method, thus making a comparison of the content. Of course, the basic type is the comparison of values, this is nothing to say.We should also note that the Java language requirements for Equals () are as follows, and these requirements must be followed:• Symmetry: If X.equals (y) returns "true", then Y.equals
"; x.equals (and x objects of different types) will always return "false"
4. Summarytherefore, for the Equals method:
It is intended to compare the content of two objects with the same
When necessary, we need to rewrite this method to avoid breaking the original intention and to follow the above principles
Iv. Methods of Hashcode1, the source of HashcodeThe Hashcode method is the ins
The public boolean equals (Object obj) in object objects, and the method returns true for any non-null reference value x and Y, when and only if X and Y refer to the same object;Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode m
Tips"Effective Java, third Edition" an English version has been published, the second edition of this book presumably many people have read, known as one of the four major Java books, but the second edition of 2009 published, to now nearly 8 years, but with Java 6, 7, 8, and even 9 of the release, the Java language has
returns the computed Hashcode value:1 public class comhashcode{2 public static void Main (string[] args) throws Exception {3 Comhashcode a = new Comh Ashcode (); 4 Comhashcode B = new Comhashcode (); 5 System.out.println (A.hashcode ()); 870919696 6 System.out.println (B.hashcode ()); 298792720 7 8 Long num1 = new Long (8), 9 long num2 = new Long (8), System.out.println (Num1.
TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified.
X,x.equals (NULL) should return FALSE for any non-null reference value.
The Equals method of object implements the most differentiated equality relationship on the object, that is, for any non-null reference value x and Y, this method returns True if and only if X and Y ref
The Equals method and the Hashcode method in Java are in object, so each object has these two methods, sometimes we need to implement the specific requirements, we may have to rewrite the two methods, today we will introduce some of the effects of these two methods.
The Equals () and
Comh Ashcode (); 4 Comhashcode B = new Comhashcode (); 5 System.out.println (A.hashcode ()); 870919696 6 System.out.println (B.hashcode ()); 298792720 7 8 Long num1 = new Long (8), 9 long num2 = new Long (8), System.out.println (num1.hashcode ()); 811 System.out.println (Num2.hashcode ()); 812 }13}Summarize:(1) binding. when t
Java --- hashCode () and equals (),1. hashCode () and equals () APIs
HashCode() AndEquals() All come from the god classObject,All classes have these two methods: special timing and re-writing.
They are used for comparison in the s
method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares that the equality object must have an equal hash code.(2) reason for binding. Hashtable implements a hash table, in order to successfully store and retrieve objects in the Hashtable, objects used as keys must implem
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.