should I pay attention to when modifying equals? A: pay attention to the following points: If you modify the equals method, modify the hashCode method. First, this is a language convention, one reason for doing this is that when this object acts as an element of the hash container, hashCode is required. The default
A little more understanding of equals () in java ()Subverted to equals (), this method is always used to distinguish from = when learning: equals () is used to compare the value of an object, = is used to compare the address. Of course, this is not an error, but it has limitations. Although the
eigenvalues here are the string type Name property, which represents the name of each person object. Since only the characteristic value that needs to be compared is set in the Equals method, so long as the name of the two Person class object is the same, their judgment is the same.2. The Hashcode method in the class needs to be overridden/overwrittenIn fact, when 1 is achieved, it is guaranteed to determi
This article describes a technique for rewriting (override) the Equals method. Using this technique, you can still meet the conventions of the Equals method even when the subclass of the entity class adds a new field (field).
In the 8th of the book Effective Java, Josh Bloch The difficulty of meeting the equals
according to the Equals method, then calling the Hashcode method of any of these two objects does not necessarily mean that the Silkworm sounds different integer results. However, programmers should know that it is possible to improve the performance of hash table by producing different integer results for unequal objects. (For example, when you have an entity that compares equality only by ID, but does no
information used in your SS comparisons on the object is modified.
? For any non-null reference value x, x. Empty SS (null) shocould return false.
The specified SS method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x = y has the value true ).
Note that it is generally necessary to override the
equality relationship between objects. That is, for any non-null reference values x and y, if and only when x and y reference the same Object, this method returns true (x = y has a value of true ).
Note: When this method is overwritten, it is usually necessary to override the hashCode method to maintain the conventional protocol of the hashCode method, which declares that equal objects must have equal hash
This method first compares whether the address of two objects is the same, and if the same directly returns True,Otherwise, (1) If the first comparison of string type is a string type, if so, then compare whether the length is the same, the same words again, each character is the same;(2) If it is a normal object, put the object's attributes into HashMap as the key, and the value in the attribute as value, as the way to compare.Hashcode () method, is the native methodFor programming languages th
, it is first to understand a problem:OneEquals () equal to two objects, hashcode () must be equal.TwoEquals () is not equal to two objects, but does not prove that their hashcode () are unequal.In other words, the Equals () method is not equal to two objects, and hashcode () may be equal. (My understanding is that the
the int type, this method is mainly to improve the performance of the hash table (such as the java.util.Hashtable provided by the hash table);By analyzing the Get () method of Hashtable, we can see that the key key must satisfy the Hashcode value and key value to obtain the corresponding value key. Public object get (Object key) { hashtableentry e; = table; int hash = Key.hashcode (); int index = (hash 0x7FFFFFFF)% tab.length; fo
"; int i = 1; String str2 = new String ("ABCD"); What is the memory address, and what is the memory address? A 16-binary shaped Value//java object memory address a hash algorithm to derive an int type number System.out.println ("abcd1" = = str1); Using the Equals method of string, the string is compared System.out.println (Str.equals ("ABCD")); In Javac, the virtua
book, it feels that the Equals () method is fragile, so the class representing the active entity does not overwrite the Equals () method, and for the value class that must write the Equals () method, try not to have an inherited relationship, because inheritance can violate the symmetry of the case, Or you can ensure that the parent class cannot generate instanc
Java and the contract to make a general summary, if there is a mistake, please correct me.The overall structure of the JUC package is generally as followsThe outer frame is mainly lock (Reentrantlock, Readwritelock, etc.), synchronizer (semaphores, etc.), blocking queues (blockingqueue, etc.), Executor (thread pool), Concurrent Containers (CONCURRENTHASHMAP, etc.), as well as fork/join framework;The inner l
The importance of the Java equals method does not need to be repeated. If you want to compare whether two objects are the same, you should implement the equals method so that the objects can be compared using conditions that you think are equal.
The following content is only the API specification, which has no profound significance. But I listed it first because
public class TestString {/** Data types in Java can be divided into two categories:* 1, the basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean* Compare them, apply the double equals sign (= =), compare their values.* 2, composite data type (Class)* When they compare with (= =), they compare their storage address in memory, so unless it's the same new object,
Translator: You might think Java is very simple, the equals implementation of object will be very simple, but the fact is not what you think, after reading this article patiently, you will find that you know so little about Java. If this article is a Java Programmer's entry test, then I don't know how many people will
type, but a cannot.) Similarly, Other encapsulation classes and basic types are also the same. in Java, the difference between equals and = compares the addresses of two objects, while equals compares the content of two objects. In JDK or later versions, the basic type and encapsulation class can be automatically converted, similar to the string type object and
, you may try. Why, then? That's because they're not comparing the values of the fields in the object or the values of the objects themselves, but the physical addresses.
Let me give you one more example.
String a = new string ("a");
String b = new String ("a");
System.out.println (A = = B);
System.out.println (A.equals (b));
When we create 2 string objects we will find that the result of the execution is false true. Why the value returned by this euqals has been programmed to true. Because at t
)); }falsefalsefalsetrueStringBuffer is also a string class, and it is important to note that it does not have the Equals method of the override base class. Therefore, Equals () returns false even if the contents of the object are the same.The integer class implements its own equals () method, and the contents of the 999 two objects are correctly judged to be
; atomicstringreference = new Atomicreferencestring> (initialreference); "new value referenced"; out.println ( "exchanged:" + exchanged); exchanged = Atomicstringreference.compareandset (Initialreference, newreference); System. out.println ( "exchanged:" + exchanged); In the example above, an initial reference was used to create a generic atomicreference. Then call two times the Compareandset () method to compare the stored reference with the initial reference. If the stored reference is eq
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.