Differences between equals and = in Java

Source: Internet
Author: User

Equals in Java is very important, and it should be different from =. Recently, I was reading sun weiqin's book on Java Object-Oriented Programming and thought it was well written. So now I want to summarize it.
The main content, and the = and equals should be listed as important comparison concepts for learning.

1. Declaration format
Public Boolean equals (Object OBJ)

The comparison rule is: if the object referenced by the OBJ parameter is the same as the current object, true is returned; otherwise, false is returned.

For example, the following two objects animal1 and animal reference different objects, so the result of comparison with = or equals () is false, while the animal1 and animal variables reference the same dog object, therefore, the result of comparison with = or equals () is true.

Animal animal1 = new dog ();
Animal animal = new CAT ();
Animal anim_3 = animal1;

Then animal1 = animal (false)
Animal1.equals (animal) (false)

Animal1 = anim_3 (true)
Animal1.equals (anim_3) (true)

Some classes in the JDK class overwrite the equals () method of the oject class. The comparison rule is: if the two objects have the same type and the content is consistent, true is returned. These classes include:
Java. Io. File, java. util. Date, java. Lang. String, packaging class (integer, double, etc)

For example
Integer int1 = new INTEGER (1 );
Integer int2 = new INTEGER (1 );

String str1 = new string ("hello ");
String str2 = new string ("hello ");

Int1 = int2 output: false, because different objects
Int1.equals (int2) Output: True

Str1 = str2 (false)
Str1.equals (str2) (true)
Of course, you can customize the equals () method that overwrites the object class and redefine the comparison rules. For example, the following equals () Comparison rule for the person class is as follows: if both objects are person classes and their attribute names are the same, the comparison result is true; otherwise, false is returned.

Public   Class  Person {  Private  String name;  Public  Person (string name ){  This . Name = Name ;}  Public   Boolean  Equals (Object O ){  If ( This = 0) Return  True  ;  If (! O Instanceof Person) Return   False  ;  Final Person Other = (Person) O;  If ( This  . Name (). Equals (other. Name ()))  Return   True  ; Else  Return   False  ;}} 

Note: When rewriting the equals method, you must satisfy the Discrete Mathematical characteristics.

1. Self-inverse: the return value of X and X. Equals (x) must be true.
2 symmetry: For any referenced values X and Y, if and only when Y. Equals (x) returns true, the return value of X. Equals (y) must be true;
3 pass: If X. Equals (y) = true, Y. Equals (z) = true, X. Equals (z) = true
4. Consistency: If the objects involved in the comparison are not changed, the results of the object comparison should not be changed.
5. non-null: the return value of any non-null reference values X and X. Equals (null) must be false.

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.