the difference between the two:
a:==
Base type: Compare whether the value is the same
Reference type: Compares whether the address value is the same
B:equals ()
Only reference types can be compared.
By default, the comparison is the value of the address is the same, because we can see the source code can see that the method in the object class is compared with = =
public boolean equals (Object obj) { return (this = = obj); }
。For example, when we want to compare some of the two reference types for equality (whether the name or age of the student class is equal), then it is meaningless to compare the address values, so we will generally override this method because the method is a method in the object class. You typically override the Equals method (Alt+shift+s) with a shortcut key. The classes in the system API override the Equals method themselves, so the direct call comparison is actually comparing the value of the object to equality, rather than comparing the object's address value. And we write the class ourselves. such as student student class, and so on, if you do not override the Equals method, the default is to compare the address value of the object, so the method is overridden to compare the value of the object. The following code:
public class Equal {public static void main (string[] args) {integer a1 = new Integer (ten); integer a2 = new Integer (10); System.out.println (A2.equals (A2));//truesystem.out.println (a1 = A2);//falsesystem.out.println ("------------"); String a = new string ("Ha"); String b = new string ("Ha"); System.out.println (A.equals (b));//truesystem.out.println (A = = B);//falsesystem.out.println ("------------");< Span style= "color: #ff0000;" >student S1 = new Student (10); Student s2 = new Student (10); Student s3 = S1; System.out.println (S1.equals (S3));//truesystem.out.println (s1.equals (S2));//falsesystem.out.println (S1 = s2);// False</span>system.out.println ("------------"); int s4 = 3;int S5 = 3;//system.out.println (S4.equals (S5));//Will Error , equals can only compare reference type SYSTEM.OUT.PRINTLN (S5 = = S4);//truesystem.out.println ("------------"); String s6 = "Ha"; String s7 =new string ("Ha"); System.out.println (S6.equals (S7));//truesystem.out.println (s6 = s7);//flasesystem.out.println ("------------"); ArrayList List1 = new ArraylisT (); List1.add ("AA"); ArrayList list2 = new ArrayList (); List2.add ("AA"); System.out.println (List1.equals (List2));//truesystem.out.println (list1 = List2);//FALSESYSTEM.OUT.PRINTLN ("---- --------"); ArrayList list3 = new ArrayList (); List1.add ("AA"); ArrayList list4 = new ArrayList (); List2.add ("BB"); System.out.println (List1.equals (List2));//falsesystem.out.println (list1 = list2);//false//== compares values in the base type, The address value is compared in the reference type}}class Student {private int age;public Student (int age) {this.age = age;} The Equals method is not overridden, it is called in object, and the default comparison address value}After overriding the Equals method in the Student class:
<pre name= "code" class= "java" >student s1 = new Student (10); Student s2 = new Student (10);
S1.equals (S2);true
Class Student {private int age;public Student (int age) {this.age = age;} Override the Equals method @overridepublic Boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return false;if (GETCL ()! = Obj.getclass ()) return false; Student other = (Student) obj;if (age! = Other.age) return False;return true;}}
<span style= "White-space:pre" ></span>student S1 = new Student (Ten) <span style= "White-space:pre" > </span>student s2 = new Student, <span style= "White-space:pre" ></span>student s3 = S1;<span Style= "White-space:pre" ></span>system.out.println (S1.equals (S3));//true<span style= "White-space:pre "></span>system.out.println (s1.equals (S2));//<span style=" color: #ff0000; " >true</span><span style= "White-space:pre" ></span>system.out.println (S1 = s2);//false
A few other methods:
A:hashcode () returns the hash value of the object, calculated based on the hash algorithm, which is related to the address value, but not the actual address value.
B:getclass () returns the byte-code file object of the object
C:finalize () for garbage collection, at an indeterminate time
D:clone () can implement the cloning of an object, including the data copy of the member variable, which is the new creation of an identical object in heap memory, but the class to be cloned must implement the Cloneable interface: implements the Cloneable interface to indicate Object.clone () Method can legitimately replicate the class instance by field. This interface is a markup interface, there is no way to tell us that the class implementing the interface can implement the object replication.
Java equals and = = Detailed