Operator '= =' and object's equals() method

Operator '= =' and object's equals() method

brief introduction

The operator "= =" compares whether the two operands are equal. The two operands can be either basic type or reference type. For example

> int a1=1,a2=3;
> boolean b1=a1==a2;//The operand of "= =" is of basic type, and the variable value of b1 is false
> String str1="Hello",str2="world";
> boolean b2=str1=str2;//The operand of "= =" is a reference, and the variable value of b1 is false

The equals() method is defined in the java.lang.Object class to compare whether two objects are equal.

1. Operator "= ="

When both sides of the operator are reference types, both reference variables must refer to the same object, and the result is true. For example:
Integer int1=new Interger(1);
Integer int2=new Interger(1);
Integer int3=int1;//int1 and int3 refer to the same object

System.out.println("int1==int2 is"+(int1==int2));
System.out.println("int1==int3 is"+(int1==int3));
 Run the above program, and the result is:
int1==int2 is false
int1==int3 is true

1.1 operators "= =" and polymorphism

   When "= =" is used to compare reference type variables, variables on both sides of "= =" must be of the same type or have inheritance relationship, that is, they are on the same inheritance branch of the inheritance tree. Otherwise, the compilation reports an error;
   Suppose there are four classes: creation, Animal, dog and cat. The inheritance relationship is shown in the figure below:


In the following code, the variable dog is declared as dog type, and the variable animal is declared as animal type. There is an inheritance relationship between them. Although there is no relationship between the variable dog 'object referenced by these two variables and Cat object, it can still be compared with "=";

Dog dog=new Dog();
Createure creature=dog;
Animial animal=new Cat();
System.out.println(dog==animal);//Legal, print flash
System.out.println(dog==creature);//Legal, print flash

In the following code variables, dog is declared as dog type and Cat is declared as Cat type. There is no inheritance relationship between Dog class and Cat class, so "= =" cannot be used for comparison:

Dog dog=new dog();
Cat cat=new Cat();
System.out.println(dog==cat);//Compile error

1.2 operator "= =" for array type

Array type is also a reference type. You can also use "= =" to compare, for example:

boolean b1=new int[4]==new long[5];//Compilation error, inconsistent types on both sides
boolean b2=new int[4]==new int[4];//b2 is false
int array1[]=new int[4];
int array2[]=array1;
boolean b3=array1==array2;// b3 is true;

2. equals() method of object

The equals() method is defined in the Object class. Its declaration format is as follows:

public boolean equals(Object obj)

The comparison rule of the Equals() method of the Object class is: when the parameter obj references the same Object as the current Object, it returns true; otherwise, it returns false

public boolean equals(Object obj)
 {
 	if(this==obj)return true;
	else return false;
 }

For example, the following animals1 and animals2 variables refer to different objects, so use the "==
"Or the comparison results of equals() method are all false, while the variables of animals1 and animals3 refer to the same Dog object, so the results of" = = "or equals() method are all true;

 Animal animal1=new Dog();
 Animal animal2=new Cat();
 Animal animal3=new animal1;
 
 System.out.println(animal1==animal2);//Print false
 System.out.println(animal1.equals(animal2));//Print false 

 System.out.println(animal1==animal3);//Print true
 System.out.println(animal1.equals(animal3));//Print true

In JDK, there are some equals() methods that cover the object class. Their comparison rules are: if the two types are the same and the content is the same, then return true; these classes include: java.io.File, java.util.Date, java.lang.String, and wrapper classes (such as java.lang.Interger and java.lang.Double);
For example, the following int1 and int2 variables refer to different Integer objects, but their contents are all 1, so the result of "= =" more is flash, but the result of using equals() method is true

Integer int1=new Integer(1);
Integer int2=new Integer(1);

System.out.println(int1==int2);Printingfalse
System.out.println(int1.equals(int2));Printingfalse

In practical application, it is usually meaningful to compare = = strings according to their contents. Therefore, the equals() method of String class should be called instead of the "=" operator. ==Programmers sometimes misuse "=" operators, such as the following programs:

String name =new String("Tom");
if(name=="Tom")//If the comparison result is false, you should change "= =" to equals, and the comparison result will be true
{
	System.out.println("Hello ,Tom");
}
else
{
	System.out.println("Sorry,I don't know you.");
}

For example, the Boolean class is a wrapper class. As long as the contents of two Boolean objects are the same, the comparison result of equals() method is true, and the following program prints C:

Boolean b1= new Boolean(true);
Boolean b2= new Boolean(true);
if(b1==b2)
	if(b1.equals(b2))
		System.out.println("a");
	else
		System.out.println("b");
else
	if(b1.equals(b2))
		System.out.println("c");
	else
		System.out.println("d");
		

The operation results are as follows:

For example, the following variables b1 and obp1 are declared as different variables, but they actually refer to the same Boolean variable, so "= =" operator and equals() method are both true. The following program prints a:

Boolean b1=new Boolean(true);
Object obj1=(Object)b1;
if(obj1==b1)
	if(obj1.equals(b1))
		System.out.println("a");
	else
		System.out.println("b");
else
	if(obj1.equals(b1))
		System.out.println("c");
	else
		System.out.println("d");

The operation results are as follows:

For example, if the Float and Double types are wrapper types, as long as the contents of two Float objects or two Double objects are the same, the comparison result of equals() method is true

Float f1=new Float("10F");
Float f2=new Float("10F");
Double d1=new Double("10D");
System.out.println(f1==f2);//false
System.out.println(f1.equals(f2));//true
System.out.println(f2.equals(d1));//false because f2 and d1 are not of the same type
System.out.println(f2.equals(new Float("10")));//Print true

The operation results are as follows:

In the last instance, variables A and b refer to different String objects, but they all contain "hello", while variable c is of String array type, so the result of a.equals © is false

String a="hello";
String b=new String(a);
char c[]={'h','e','l','l','o'};

System.out.println(a=="hello");//Print true
System.out.println(a==b);//a and b refer to different objects, print false
System.out.println(a.equals(b));//Print true
System.out.println(a.equals(c));//a and c are not the same type, print false

summary

The operands on both sides of the "=" operator can be basic types or reference types. When both operation elements are reference variables, the two reference variables must be of the same type or have inheritance relationship. Otherwise, compilation fails. When both reference variables refer to the same object, the result is true. Otherwise, the result is false
Generally speaking, when the reference objects of a.equals(b), a and B reference variables are the same and the contents are the same, return true; otherwise, return false

This article reference book: Java object-oriented programming (Second Edition) - Sun Weiqin

Published 2 original articles, praised 0 and visited 35
Private letter follow

Tags: Java JDK Programming

Posted on Sun, 19 Jan 2020 08:57:01 -0500 by megavolt