= =

For basic data types, it’s all about matching values.

public class Equals { public static void main(String[] args) { int a = 1; int b = 1; System.out.println(a == b); System.out.println(b == a); }}Copy the code

The following output is displayed:

true
true
Copy the code

For reference types, == matches whether two references refer to the same memory region.

public class Equals { static Person person1 = new Person(); static Person person2 = new Person(); public static void main(String[] args) { System.out.println(person1 == person2); }}Copy the code

The above two objects are created by the new keyword respectively. At this time, we use == to compare, and the result is as follows

false
Copy the code

The object creation is only involved here, which will be reflected in the virtual machine notes.

equal

Equals is the parent of all objects in Java, a method defined by the Object class. It can only compare objects, and it indicates whether the values of both references are equal.

public class Equals { public static void main(String[] args) { Person person1 = new Person(); Person person2 = new Person(); person1.setName("1"); person2.setName("1"); System.out.println(person1.getName().equals(person2.getName())); }}Copy the code

Equals as a comparison between objects has the following properties

  • reflexivityFor any non-empty reference to x, x.equals(x) should return true.
  • symmetry: For any non-empty reference x and y, if x.evers (y) is true, then y.evers (x) is also true.
  • transitivity: For any non-null reference value, there are three values, x, y, and z. If x.quals (y) returns true and y.quals (z) returns true, then x.quals (z) should return true as well.
  • consistency: For any non-empty reference x and y, x.equals(y) must always be equal if they are equal.
  • Non empty: for any non-null reference value x, x.equals(null) must return false.

The equal implementation of String

String is also one of the most commonly used modifiers in everyday programming. Unlike the other basic data types, String itself is final and cannot be inherited by other objects. It can’t be changed once assigned. It inherits from Object, so all the capabilities Object has can be found in String.

We’re going to focus here on the equal implementation in String.

Public Boolean equals(Object anObject) {// If (this == anObject) {return true; If (anObject instanceof String) {String anotherString = (String)anObject; int n = value.length; / / whether the length is equal, if a certain to false if (n = = anotherString. Value. Length) {char v1 [] = value; char v2[] = anotherString.value; int i = 0; // Start character by character comparison. If unequal characters are found, return false while (n--! = 0) { if (v1[i] ! = v2[i]) return false; i++; } return true; } } return false; }Copy the code

The implementation of equals in String can be summarized as follows:

1. Check whether itself is passed in. If so, return true.

2. Check if it is a String. If not, return true.

2.1 If it is a String, the len passed in is compared to see if it is consistent. If so, the comparison continues.

2.2 Compare characters, return false if the characters are inconsistent, and continue until the comparison is complete (here is a circular full match)

Here’s another reminder, you might be wondering when

if (this == anObject) {
  return true;
}
Copy the code

How does this statement return true? Strings compare heap space and, on first glance, seem to never walk, but you forgot that the string.intern () method represents concepts that differ between JDK versions

In JDK1.7 and later, we call intern to determine if the specified string exists in the runtime constant pool. If not, we add a reference to the string to the constant pool instead of copying an object into it.

Override HashCode

The equals method and hashCode are both methods defined in Object and are often overridden together.

The equals method is used to compare whether objects are equal in size, and the Hashcode method is used to determine the hash value of each object. Overwriting equals without overwriting hashcode is likely to result in two different objects with equal Hashcodes, causing a conflict. Such as

String str1 = "Call "; String str2 = ""; System.out.println(str1.hashCode()); System.out.println(str2.hashCode());Copy the code

The hashCode represented by the above two strings is identical.

1179395, 1179395,Copy the code

Hashcode is equal to hashCode, but not equals.

The following is a summary of equals

  • If you call the hashCode method on the same object during Java runtime, you should always return the same hashCode no matter how many times you call it, but executing the hashCode method may not return the same value across different Java programs.
  • If two objects have equal equals, then hashCode must be the same
  • If two objects are not equal to equals, then hashCode may also be the same, so you need to rewrite the hashCode method because you don’t know the underlying construction of hashCode (you can later parse the JVM source code to learn more about the construction of hashCode). So you need to rewrite the hashCode method to generate different hashCode values for different objects, which can improve the speed of accessing different objects.
  • HashCode is usually implemented by converting addresses to integers.

References: github.com/crisxuan/be…