Explain the difference between java== operator and equals of method in detail

  • 2021-07-09 07:59:16
  • OfStack

There are two ways to judge whether two variables are equal in Java language program: 1 is to use the = = operator, 2 is to use equals method.

1. = = Operator

For the == operator, if two variables are primitive and numeric, true is returned as long as their values are equal; However, if you have two variables of reference type, there are two scenarios: 1) They point to the same object and return true; 2) They point to different objects and return false even if the objects have the same content; The following program demonstrates the result of comparing two variables for equality using the == operator:


public class Test {
 public static void main(String[] args) {
  int a = 5;
  int b = 5;
  String s1 = "helloJava";
  String s2 = "helloJava";
  String s3 = "hello"+"Java";
  String s4 = "hello";
  String s5 = "Java";
  String s6 = s4 + s5;
  String s7 = new String("helloJava");
  System.out.println("a==b?: "+(a==b));
  System.out.println("s1==s2?: "+(s1==s2));
  System.out.println("s1==s3?: "+(s1==s3));
  System.out.println("s1==s6?: "+(s1==s6));
  System.out.println("s1==s7?: "+(s1==s7));
  System.out.println("s1.equals(s6)?: "+(s1.equals(s6)));
  System.out.println("s1.equals(s7)?: "+(s1.equals(s7)));
 }
}

The running result is:

a==b?: true
s1==s2?: true
s1==s3?: true
s1==s6?: false
s1==s7?: false
s1.equals(s6)?: true
s1.equals(s7)?: true

Result analysis: a and b of two int types are equal and need no more explanation; The strings referenced by s1, s2, and s3 are determined during compilation, so they all refer to the same string object in the constant pool; The string value corresponding to s6 cannot be determined during compilation; s7 uses the new constructor to create a new String object. s7 refers to the String object created in heap memory and is not in the constant pool. (The creation and storage mechanism of String string will be introduced in the next article. I hope you can set the top WeChat official account and watch it in the first time).

2. equals () method

equals () method is a method provided by Object class. There is no difference between using this method to judge the equality of two objects directly and using the == operator, but how to implement the judgment rule similar to "value equality"? The answer is to override the equals method.

String has overridden the equals () method of Object, and the equals () method of String determines the equality of two strings by comparing them with the equals () method if the two strings contain equal sequences of characters and returning true if not false.

1 In general, overriding the equals () method should meet the following conditions:

1) Reflexivity: For any x, x. equals (x) 1 returns true.
2) Symmetry: For any x, y, if x. equals (y) returns true, y. equals (x) also returns true.
3) Transitivity: For any x, y, z, if x. equals (y) returns true and y. equals (z) also returns true, x. equals (z) also returns true.
4) Identical: For any x, y, if the information used for comparison in the object does not change, then no matter how many times x. equals (y) is called, the returned result should remain 1, either 1 straight back to true or 1 straight back to false.
5) For any x that is not null, x. equals (null) 1 returns false.

In practical applications, it is often necessary to rewrite the equals () method according to the business itself, and the implementation of the equals () method is also determined by the business. Here is an example of rewriting the equals () method in line with the actual situation:


public class Person {
 private String name;// Name 
 private int age;// Age 
 private String idNum;// ID number 
 
 public Person() {
  super();
 }
 
 public Person(String name, int age, String idNum) {
  super();
  this.name = name;
  this.age = age;
  this.idNum = idNum;
 }

 // Rewrite equals Method 
 public boolean equals(Object obj){
  // If two objects are the same 1 Objects that return true
  if(this == obj){
   return true;
  }
  //obj Yes Person Object 
  if(obj!=null && obj.getClass()==Person.class){
   Person p = (Person)obj;
   // And the ID number is the same before judging that the two objects are equal 
   if(this.idNum.equals(p.getIdNum())){
    return true;
   }
  }
  return false;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getIdNum() {
  return idNum;
 }

 public void setIdNum(String idNum) {
  this.idNum = idNum;
 }

 public static void main(String[] args) {
  Person p1 = new Person(" Zhang 3", 14, "zs14abc");
  Person p2 = new Person(" Zhang 32", 15, "zs14abc");
  Person p3 = new Person(" Zhang 3", 14, "zs14abc2");
  System.out.println("p1.equals(p2)?: "+(p1.equals(p2)));
  System.out.println("p1.equals(p3)?: "+(p1.equals(p3)));
 }

}

Run results:

p1.equals(p2)?: true
p1.equals(p3)?: false

Do you understand the difference and usage between = = and equals? Many people on the Internet say that the equals () method is to judge the values of two objects are equal, which is not very accurate, because you can override the equals () method, and you can make them equal as you want. In extreme cases, you can make Person objects equal to Pig objects.

Therefore, in order to facilitate beginners' memory, it can be said that the = = operator is used to compare whether the values of two variables are equal; The equals () method is used to compare the contents of two objects.


Related articles: