What is the difference between equals and = = in Java?

Source: Internet
Author: User

Equals in Java is very important, and = to differentiate, Sun Weichen's Java Object-oriented Programming book explains this, and now summarizes its
The main content, and to include = and equals as important contrasting concepts to learn

1. Declaration format
public boolean equals (Object obj)

The comparison rule is that when the object referenced by the parameter obj is the same object as the current object, it returns True, otherwise false.


For example, the following two objects, Animal1 and Animal2, refer to different objects, so the result of comparison with the = = or the Equals () method is false, whereas the Animal1 and ANIMAL3 variables refer to the same dog object, so use = = or equals () The result of a method comparison is true.

Animal animal1=new Dog ();
Animal animal2=new Cat ();
Animal Animal3=animal1;

Then Animal1==animal2 (FALSE)
Animal1.equals (Animal2) (false)

Animal1==animal3 (True)
Animal1.equals (ANIMAL3) (true)


There are classes in the JDK class that cover the Equals () method of the object class, and the rule is: if the two objects are of the same type and the content is consistent, return True, these classes are:
Java.io.file,java.util.date,java.lang.string, packing class (integer,double, etc.)

Like what
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 overrides the object class to redefine the comparison rule. For example, the Equals () comparison rule for the person class below is: As long as two objects are the person class, and their property name is the same, the comparison result is true, otherwise the return is False

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 that when overriding the Equals method, you should be careful to satisfy the discrete mathematical characteristics

1. Reflexivity: The return value of X,x.equals (x) for any reference value must be true.
2 symmetry: For any reference value x, Y, the return value of X.equals (Y) must be true if and only if Y.equals (x) returns a value of true;
3 transitivity: If X.equals (y) =true, y.equals (z) =true, then X.equals (z) =true
4 Consistency: If the object participating in the comparison does not change, then the result of the object comparison should not have any change
5 non-nullability: any non-null reference value x,x.equals (NULL) The return value must be false

What is the difference between equals and = = 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.