The equals and = =

== operator:

Java data types can be divided into two types:

1. Basic data types
  • Byte (8 bits), short (16 bits), int (32 bits), Long (64 bits), double (64 bits), Boolean, float (32 bits), char (16 bits) — a byte is 8 bits
  • 1. The JVM does not provide bytecode instructions specific to booolean types, but instead uses int-related instructions. 2. Access and modify Boolean arrays will share the Baload and Bastore instructions of byte arrays.

Basic data types use == to compare their values

2. Reference types

== compares the addresses they hold in memory, so returns false unless they are the same object

Objects are stored on the heap, and references to objects are stored on the stack. “==” compares values in the stack. To compare the contents of objects in the heap are the same, you need to override equals

The equals method:

By default (no rewriting of equals), equals compares objects’ memory address to the same address, and the eqUSAL method is used as ==

     public boolean equals(Object obj) {
        return (this == obj);
    }
Copy the code

If you override equals, you need to determine whether the contents of the objects are equal

Public Boolean equals(Object anObject) {return true if (this == anObject) {return true; } // Check whether the type is String, otherwise return false if (anObject instanceof String) {String anotherString = (String) anObject; int n = length(); If (n == anotherString.length()) {int I = 0; While (n--! = 0) { if (charAt(i) ! = anotherString.charAt(i)) return false; i++; } return true; } } return false; }Copy the code

Equals method in the TextUtils utility class

Public static Boolean equals(CharSequence a, CharSequence b) {if (a == b) return true; int length; If (a! = null && b ! = null && (length = a.length()) == b.length()) {// Call String's equals if (a instanceof String &&b instanceof)  String) { return a.equals(b); } else {// for (int I = 0; i < length; i++) { if (a.charAt(i) ! = b.charAt(i)) return false; } return true; } } return false; }Copy the code

Comparing the two overridden equals methods, we first compare the “==” memory address, then check whether the object is empty and of the same type, and then compare it according to specific conditions

The equals method has a few features that need attention

  • Reflexivity: a.equals(a) returns true
  • Symmetry: a. quals(b) returns true then B. quals(a) returns true
  • Transitivity: A. quals(b) B. quals(c
  • Consistency: If nothing has changed in the comparison, the result of the comparison should not change
  • Non-null: Returns false for any non-empty object a.eq (null)

hashcode

1. What are hash and hash tables?

A hash is a function, and the implementation of that function is an algorithm, which uses a series of algorithms to get a hash value. At this point, we need to know another thing, the hash table, and the hash value that we get from the hash algorithm is in this hash table, which means that the hash table is made up of all the hash values, and there are multiple hash functions, which means there are multiple algorithms that get the hash value.

2, hashcode

A hashcode is something that you get from a hash function, or more generally, an algorithm, and a hashcode is something that has a place in a hash table.

Every object has a Hashcode, so where did you get the hashcode of that object?

First of all, an object must have a physical address. In other posts, hashcode is used to refer to the address of the object, which is certainly misleading. The physical address of the object is not the same as this Hashcode address, which refers to the location of the object in the hash table. A physical address is the address of an object stored in memory. How does an object get hashcode?

The internal address (that is, the physical address) of the object is converted to an integer, which is then used by the hash function algorithm to get hashCode. So, what is hashcode? It’s the corresponding position in the hash table.

Here, for example, if it’s not clear, hashcode is 1, hashcode is 2, (…) 3, 4, 5, 6, 7, 8, there is an object A, the physical address of A is converted to an integer 17(this is if), through the direct mod algorithm, 8=1, then the hashcode of A is 1, and A is at the position 1 in the hash table.

3. How does equals relate to hashCode?

  • If two objects are equal to equals, then they must have the same HashCode
  • 2. If two objects have the same HashCode, it does not mean that the two objects are identical. It only means that the two objects are stored in the same location in the hash storage structure

4. Why is it recommended to rewrite hashCode as well as equals?

class A { private String name; private int age; Public Boolean equals(Object o) {if (this == o) return true; if (! (o instanceof A)) return false; A a = (A) o; return age == a.age && Objects.equals(name, a.name); } public int hashCode() {return objects.hash (name, age); }}Copy the code

After overwriting equals, try overwriting hashcode so that they have the same hashcode value when equals equals.