The Equals method in Java

Source: Internet
Author: User

First, the Equals method introduction

1.1. Use the following example to master the usage of equals
1 package cn.galc.test; 2  3 public class Testequals {4 public     static void Main (string[] args) {5         /** 6          * Use the construction method here Cat () new out in heap memory Two cats, 7 * The          color,weight,height of these two cats are the same, 8          * But C1 and C2 will never be equal, because C1 and C2 are the reference objects of two cats in the heap memory respectively, 9          * It contains the address of the two cats, but because two cats are stored in the heap memory in two different spaces,          so C1 and C2 each have different addresses, so C1 and C2 will never be equal.          */12         Cat C1 = new Cat (1, 1, 1),         cat C2 = new Cat (1, 1, 1);         System.out.println ("The result of C1==C2 is:" + (c1== C2);//false15         System.out.println ("C1.equals (C2)" Results are: "+c1.equals (C2));//false16     }17}18 class Cat {20     int color, weight, height;21 public     Cat (int color, int weight, int height) {         This.color = Color;24
   this.weight = weight;25         this.height = height;26     }27}
1.2. Draw a memory analysis diagram to analyze the results of C1 and C2 comparisons

Program:

Cat C1 = new Cat (1,1,1);

Cat C2 = new Cat (1,1,1);

The layout of the memory after execution is as shown,

C1 points to an object, C2 also points to an object, and C1 and C2 contain the addresses stored in the heap memory of the two cat objects, because the two cat objects are located in different storage spaces, so the addresses contained in C1 and C2 are definitely not equal, So C1 and C2 are certainly not equal to the two reference objects. Therefore execution: "System.out.println (C1==C2);" The printed result is definitely false. So you new out of two objects, you can rest assured that the two objects are never the same reference, the same words will be one to cover out, this is not. C1 is not equal to the C2 comparison is C1 and C2 the contents of the two references, because the new two objects of their reference will never be the same, so C1 and C2 the contents of the two references will never be the same, so C1 can never be equal to C2. Therefore, by comparing the references of two objects, it is never possible to make two objects equal, identical.

  To determine whether two objects are equal, it is not possible to compare the references of two objects for equality, which is never equal, because two objects are never equal, so the correct comparison method is to directly compare the two objects, comparing the two objects is not the same as the essence, That is, the contents of these two objects are not the same, and determine whether the two objects are equal by comparing their property values.

The object class provides a Equals () method to compare the contents of two objects, so we can use this method to compare whether two objects are logically "equal". such as: C1.equals (C2); Here is a call to the Equals () method inherited from the object class, and the Equals method in the object class is defined by the Lookup API documentation as follows:

public boolean equals (Object obj)

The default implementation of the Equals () method provided in the object class is to compare references to the current object and the one you want to compare to the same object that they are pointing to, that is, the same as "C1==c2", "C1.equals (C2)" and "C1==C2" is completely equivalent. So directly using the inherited Equals () method is also unable to directly compare the contents of two objects is the same, for this reason, we have to rewrite the Equals () method to change the default implementation of this method.

In the Cat class below, rewrite the inherited equals () method :

 1 class Cat {2 int color, weight, height; 3 4 public Cat (int color, int weight, int height) {5 this.co Lor = color; 6 this.weight = weight; 7 This.height = height; 8} 9 10/**11 * Here is the Equals () method that overrides equality inherited from the object class, changing the default implementation of this method, 12 * Determining whether two objects are logically equal by the implementation of our own definition. 13 * Here we define if two cats have the same color,weight,height, 14 * Then we think the two cats are logically identical, that is, the two cats are "equal".         */16 public boolean equals (Object obj) {obj==null) {false;19}20 Else{21/**22 * instanceof is an object operator. 23 * The object operator is used to determine whether an object belongs to an instance of a specified class or a specified subclass. 24 * The object operator is a combined word instanceof. 25 * The operator is a binocular operator whose expression on the left is an object, the expression on the right is a class, and 26 * If the object on the left is the object created by the right-hand class, the result of the operation is true, otherwise false. */28 if (obj instanceof cat) {Cat c = (cat) obj;30 if (C.colo   R==this.color && c.weight==this.weight && c.height==this.height) {31                  return true;32}33}34}35 return false;36}37} 

At this point the Print command is executed inside the main method again:

1 public static void main (string[] args) {2         /** 3          * Use construction method here Cat () in heap memory new out two cats, 4          * These two cats Color,weight,heigh T are all the same, 5          * But C1 and C2 are never equal, this is because C1 and C2 are the reference objects of two cats in the heap memory, 6          * can find the address of these two cats, but because two cats are stored in the heap memory in two different spaces, 7          * So C1 and C2 each have different addresses, so C1 and C2 will never be equal. 8          */9         Cat C1 = new Cat (1, 1, 1),         cat C2 = new Cat (1, 1, 1),         one System.out.println ("c1==c2 Result:" + (C1==c 2);//false12         System.out.println ("C1.equals (C2)" Results are: "+c1.equals (C2));//true13     }

This time the result is different from the result of the last time the Equals () method was rewritten:

  "System.out.println (c1 = = C2);" The printed result is still false, because this is the content of the reference to the comparison of two objects, the contents of which are of course unequal, and never equal, so the printed result is definitely false.

  "System.out.println (c1.equals (C2));" The printed result is true, because we rewrite the Equals () method in the Cat class, changing the default implementation of this method, we change the implementation of the method to as long as the two objects are real, and are all cats, and their color (color), height (height and weight (weight) are the same, then the two cats are logically identical, are exactly the same two cats, that is, the two cats are "equal". So the result printed here is true.

1.3. How do I compare the equality of two string objects?

Look at the following example:

1 public class Testequals {2     3 public     static void main (string args[]) {4         string s1 = new String ("Hello"); 5         String s2 = new string ("Hello"), 6         System.out.println ("S1 = = S2 Result:" + (S1 = = s2));//false7         System.out.println (" The result of s1.equals (S2) is: "+s1.equals (S2));//true8     }9}

This time is a comparison of two string objects for equality:

System.out.println (S1 = = s2);

The printed result is still fase, because this compares the references of the S1 and S2 two string objects, the references of two objects are never equal, so the printed result is false.

System.out.println (s1.equals (S2));

The printed result is true because the string class overrides the inheritance from the object class (all classes are inherited from the object class, and of course the string class is no exception, inheriting from the parent class has all the properties and methods of the parent class. So there is the Equals () method inside the Sting class, and the Equals () method, which is overridden by the inherited Equals () method, changes the default implementation of this method,

In the string class, this overrides the implementation of the Equals () method: Comparing the current string object with the specified string object, the specified string object cannot be empty and the character sequence of the object is the same as the string sequence of the current string object, if these conditions are met, Then the two string objects are equal.

So the S2 here has met the conditions, so the printed result is true.

Later in a class to compare whether two objects are equal, first go to the API document to find whether the class overrides the Equals () method inherited from the object class. If you override the Equals () method, then when you compare the equality of two objects, the next Equals () method is called, and if there is no Override, the call is called directly from the Equals () method inherited from the object class, and Equals () is used The default implementation of the method is to compare whether two objects are equal. Each class can therefore override the Equals () method inherited from the object class as needed.

For a class in the API document, if a class can be used without introducing a package, then this class must be in Java.lang this package, such as the string class here, can be used directly, so the string class must be in Java.lang this package. When using a class to see which package the class introduced, and then go to this package to find this class, do not introduce the package class must be located in the Java.lang inside, directly to Java.lang inside to find it.

Summary: Compare two objects for equality, and we use the Equals () method to determine whether the condition of two objects being equal is defined by the implementation of the Equals () method, which allows for more flexibility in using equals () Method compares two objects under the same class in different classes to see if they are equal.

The Equals method in Java

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.