1. For basic types (byte, short, int, long, char, float, double, boolean)
The = = operator compares the values of two numbers and cannot compare equals() because it is not an Object and equals() is a method of the Object class
2. For reference type
2.1 = = address comparison
Customize an Animal class
1 public class Animal { 2 3 publicString name; 4 5 public Animal(){ 6 } 7 8 Animal(String name) { 9 this.name = name; 10 } 11 }
Create an Animal object and compare = = with
1 public class DemoEquals { 2 public static void main(String[] args) { 3 Animal cat1 = new Animal("Silly cat"); 4 Animal cat2 = new Animal("Silly cat 2"); 5 System.out.println("cat1: " + cat1); 6 System.out.println("cat2: " + cat2); 7 System.out.println(cat1 == cat2); 8 } 9 }
Operation results
Analysis
cat1 == cat2, compared with the address value, the address value is different from the running result, so cat1 == cat2 is false;
Now let's verify,
1 public class DemoEquals { 2 public static void main(String[] args) { 3 Animal cat1 = new Animal("Silly cat"); 4 Animal cat2 = new Animal("Silly cat 2"); 5 System.out.println("cat1: " + cat1); 6 System.out.println("cat2: " + cat2); 7 System.out.println("========"); 8 cat1 = cat2; // Give Way cat1 Objects and cat2 Objects share a block of memory 9 System.out.println("cat1: " + cat1); 10 System.out.println("cat2: " + cat2); 11 System.out.println(cat1 == cat2); 12 } 13 }
Operation results
Analysis
It can be seen that after cat1 = cat2 is executed, the address values of cat1 and cat2 are the same, and the result of = = comparison is true. So in the application type, = = is used for address comparison.
2.2 equals() comparison
First of all, we need to know that equals(Object obj) is a method defined by the Object class. All our custom classes inherit this Object class, so we also have the equals() method. Let's first see how this method is defined in the Object class
This represents the object that currently uses the equals method. obj is the object of the parameter we passed. But it's a = = comparison. Shouldn't it be an address comparison. Why can we compare values with equals() in a String object? Because this method is overridden in the String class, the equals() method is used for address comparison by default. After rewriting in the String class, the value comparison is used. Let's see how to rewrite this method in the String source code.
It's good to understand this code. It doesn't matter if you don't understand it. As long as you know, the equals() method is rewritten in the String class, so you can compare values. Otherwise, the address comparison is performed by default. We can give you an example
Create an Animal class
1 public class Animal { 2 3 publicString name; 4 5 public Animal(){ 6 } 7 8 Animal(String name) { 9 this.name = name; 10 } 11 }
Then create an Animal object to compare equals
public class DemoEquals { public static void main(String[] args) { Animal cat1 = new Animal("Silly cat"); Animal cat2 = new Animal("Silly cat 2"); System.out.println("cat1: " + cat1); System.out.println("cat2: " + cat2); System.out.println(cat1.equals(cat2)); System.out.println("========"); cat1.name = cat2.name; // Change name,Make the name equally System.out.println("cat1: " + cat1); System.out.println("cat2: " + cat2); System.out.println(cat1.equals(cat2)); } }
Operation results
Analysis
It can be seen that the comparison result is the same as the previous = = comparison formula. Although their names are the same, the comparison result is still false. Because the equals() method is not overridden in our Animal Object, it automatically inherits the equals() method in the Object. The address value is the same as the comparison result
Now we override the equals() method in the Animal class
1 @Override 2 public boolean equals(Object obj){ 3 // Rewrite Object Class equals Method 4 if (obj == this) return true; 5 if (obj == null) return false; 6 if (obj instanceof Animal){ 7 Animal animal = (Animal)obj; // Type strong turn 8 // because name yes String Reference type, so proceed directly equals Comparison is to compare values 9 return animal.name.equals(this.name); 10 } 11 return false; 12 }
Then we run the above example again
Operation results
This time, you can see that after overriding the equals() method, the result of the comparison is the result of the value comparison.
3. summary
3.1 in basic types:
= = the address comparison is performed, and the equals() comparison cannot be performed, because equals() is a method defined in the Object, which can only be used by reference objects
3.2 in reference types:
= = is an address comparison. By default, equals() performs address comparison. If the class used overrides the equals() method, it depends on the comparison used by the source code it rewrites. For example, String class is used for value comparison.