2) = and equals in Java, equals in java
= And equals in Java
1. If the Compare object is a value variable: only use = 2. If the Compare object is a reference variable: =: compare whether two references point to the same object instance. Equals: first, the = operation is directly called for equals in the Object class. If a custom class inherits from an Object and does not overwrite the equals method, the equals operation is the same as that of the Object class. It only calls the = operation directly. If a class overwrites the equals method (or inherits from a class that overwrites the equals method), the effect is different from the = operation. If it is a class defined by yourself, compared with custom classes, equals and = are the same. They both compare the handle address, because the custom class inherits from the object, and the equals in the object is implemented with =. Check whether two referenced variables belong to the same Class: instanceof System. out. most classes in the println (obj1 instanceof Student) & (obj2 instanceof Student) API override the equals method. For example, String type
1> String type comparison:==: Compares whether two str s point to the same object instance. Equals: To compare whether the content of two 'strs' is the same for the comparison of 'string', there is also the public String intern () method. When the intern method is called, if the pool already contains a String equal to this String Object (the Object is determined by the equals (Object) method), the String in the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows s and t for any two strings. if and only when s. equals (t) is true, s. intern () = t. intern () is true. 1 String a = new String ("AB"); 2 String B = new String ("AB ");
3 String c = "AB ";
4 String d = "a" + "B ";
5 String e = "B ";
6 String f = "a" + e;
8 System. out. println (B. intern () = );
9 System. out. println (B. intern () = c );
10 System. out. println (B. intern () = d );
11 System. out. println (B. intern () = f );
12 System. out. println (B. intern () = a. intern ());
Running result: false true
The running result shows that B. intern () = a and B. intern () = c.
String objects created using new do not enter the string pool,
When a string is added together, the results of all static strings are added to the string pool. If the string contains variables (such as e in f), the string pool is not entered.
Assign a value when defining a variable. If the value is a static string, the operation is performed to enter the string buffer pool. If the pool contains this string, a reference is returned.
2> comparison of data type encapsulation classes
Java provides an encapsulation class for each simple data type. Each basic data type can be encapsulated into an object type. In addition to int (Integer) and char (Character), the first letter of other types is an encapsulation class name. Double (Double), float (Float), long (Long), short (Short), byte (Byte), boolean (Boolean ).
Taking int and Integer as examples to illustrate the differences between int and Integer in Java:
1.int is the basic data type. The default value can be 0. Integer is the int encapsulation class. The default value is null;
Both 2.int and Integer can represent a certain value, but int and Integer cannot be used with each other because they have two different data types;
Int a1 = 1;
Int a2 = 1;
Integer b1 = new Integer (1 );
Integer b2 = new Integer (1 );
Answer:
A1 = a2 this is an established one. It's very simple and we all know it.
A1 = b1 this is not true. The expression value is false. They are different data types (true in JDK or a later version)
B1 = b2 this is also not true. the expression value is false. Although it is of the same data type, they are two objects. = compares the addresses of two objects, and their addresses are not equal, the same content is 1;
B1.equals (b2) = true this is true, and the expression value is true. for the same data type, two objects have different addresses and the content is the same. quals compares the content of two objects, so it is true.
(A. equals (B). Since equals compares two objects, neither a nor B can be of the basic data type. Otherwise, a compilation error occurs .) (In jdk1.5 or later versions, B can be the basic data type, but a cannot.) Similarly, Other encapsulation classes and basic types are also the same.
In JDK or later versions, the basic type and encapsulation class can be automatically converted, similar to the String type object and String constant.
Integer i1 = 123;
Integer i2 = 123;
Int I = 123;
Integer i3 = new Integer (123); Integer i4 = new Integer (123 );
System. out. println ("i1 = i2 =" + (i1 = i2 ));
System. out. println ("i1.equals (i2) =" + (i1.equals (i2 )));
System. out. println ();
System. out. println ("i3 = i4 =" + (i3 = i4 ));
System. out. println ("i3.equals (i4) =" + (i3.equals (i4 )));
System. out. println ();
System. out. println ("i2 = i4 =" + (i2 = i4 ));
System. out. println ("i2.equals (i4) =" + (i2.equals (i4 )));
System. out. println ();
System. out. println ("I = i2 =" + (I = i2 ));
System. out. println ("i1.equals (I) =" + (i1.equals (I )));
System. out. println ();
System. out. println ("I = i4 =" + (I = i4 ));
System. out. println ("i4.equals (I) =" + (i4.equals (I )));
Answer:
I1 = i2 = true
I1.equals (i2) = true
I3 = i4 = false
I3.equals (i4) = true
I2 = i4 = false
I2.equals (i4) = true
I = i2 = true
I1.equals (I) = true
I = i4 = true
I4.equals (I) = true
3> hashcode and equals of the object1) to understand the role of hashcode, we can understand it in java. lang. Object. Every time a new JVM Object is created, it will throw this Object to a Hash table. The next time you compare an Object or retrieve this Object, it will retrieve this Object from the Hash table based on the hashcode of the Object. The purpose is to improve the efficiency of object retrieval. The specific process is as follows:
1. new Object (), JVM puts the Hashcode value of this Object into the corresponding Key of the corresponding Hash table. If the Hash key is the same, the conflict occurs, in this case, a linked list is generated for the Hash key, and all objects that generate the same hashcode are put on this single-chain table.
2. when comparing two objects, we first use their hashcode To Go To The hash table to find their objects. When the hashcode of the two objects is the same, they are put on the same key in the Hash table, then they must be on the linked list of the key. In this case, you can only compare whether the Object is equal based on the equal method of the Object. When the hashcode of two objects is different, they certainly cannot be equal.
The equals method of two equal objects must be true, but the two objects with the same hashcode are not necessarily equal objects. 2) When overwriting equals, it is always necessary to overwrite hashcode. If hashcode is not overwritten, this may happen. The equals method after two objects overwrite returns true, however, the hashcode method returns false, and the optimization process in execution is that the hash values of the two objects are compared before the equals method is called. If they are different, equals will not be compared. Therefore, overwriting the equals method without overwriting the hashcode method poses the above risks. The comparison should be made back to the true value of equals, but false is returned when the hash value is compared in the pre-execution.