Object:equals()、hashCode()、toString()

In Java, the Object class is the ancestor of all classes, and they all directly or indirectly inherit Object. If a class does not use the extends keyword to inherit another class, the Object class is inherited by default.

Several important methods of the Object class:

1、equals()

Not much nonsense, just look at the source code,

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

If we do not override equals(), the object will call the equals() method of the parent class Object to determine whether the memory address of the calling object and the passed parameter object are equal ( == Used to compare the memory address of the reference type data Equal, if not equal, it means two objects ).

The String class rewrites the equals() method , which can be used for string comparison. The source code is as follows:

	 public boolean equals(Object anObject) {
    
    
        if (this == anObject) {
    
    
            return true;
        }
        if (anObject instanceof String) {
    
    
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
    
    
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
    
    
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

2、toString()

This method is used to print the string form of the object.

If the toString method is not overridden, when printing the object, the toString method of the Object class will be called and the address of the object will be printed. Look at the source code:

    //Object中的toString()方法
    public String toString() {
    
    
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

The String class overrides the toString() method

	public String toString() {
    
    
        return this;
    }

3、hashCode()

  •  同一个对象生成多次hash值,值一定是一样的
    
  •  不同对象,生成的值,可能相同,产生哈希冲突,需要用equals()方法,比较哈希值相同的对象的内容是否一样
    
  •  在java中可以通过 hashCode和equals来确定对象的唯一性
    

The connection between equals() and hashCode():

  • (1)由于Object类的equals()用来比较内存地址,如果两个对象的equals()结果为true,那么这两个对象的hashCode一定相同;
    
  • (2)两个对象的hashCode()结果相同,并不能代表两个对象的equals()一定为true,只能够说明这两个对象在一个散列存储结构中。
    
  • (3)如果对象的equals()被重写,那么对象的hashCode()也要重写。
    

Why rewrite equals() to rewrite hashCode()?

Take the hashSet collection () as an example, this collection is not allowed to have duplicate elements. If we just rewrite equals() without rewriting hashCode(), the collection elements will not be unique. Because we first calculate the hash value of the object according to the hashCode() method, if the hash value is different, it is definitely not an object, so we don’t need equals() to compare; if the hash values ​​are equal, we are using equals() to determine the content Are they equal. If you don’t rewrite hashCode(), the hash values ​​of the two objects are definitely different (the object memory address returned by the hashCode() of the Object class will never be the same), that is, the two objects are not in the same hash area, and neither Need equals() comparison, just save it directly.

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/112792072