Introduction to zero basics of Java 12: Object class in Java

Keywords: Java Back-end

🍅 There are benefits and surprises every week Nezha community - fenghuolun project

🍅 Supporting articles for Java learning route: Summary of java learning route, brick movers counter attack Java Architects (the strongest in the whole network)

🍅 Classic Java interview questions: Summary of 208 classic Java interview questions with 100000 words (with answers)

🍅 Introduction: high quality creator in Java field 🏆, CSDN the author of the official account of the Na Zha ✌ , Java Architect striver 💪
🍅 Scan the QR code on the left of the home page, join the group chat, learn and progress together
🍅 Welcome to praise 👍 Collection ⭐ Leaving a message. 📝

1, Introduction to Object class

The Object class is the core class under the Javajava.lang package. The Object class is the parent class of all classes. If a class does not explicitly inherit a parent class, it is a subclass of Object;
All classes in Java inherit the Object class by default.


public class Student {
    private Integer id;
    private String name;


public class Student extends Object{
    private Integer id;
    private String name;

The effect of the two is the same.

2, Object provides 11 methods


protected native Object clone() throws CloneNotSupportedException;

This method can be called only when the clonable interface is implemented. Otherwise, clonnotsupportedexception will be thrown.


public final native Class<?> getClass();

The final method returns an object of Class type and obtains the object through reflection.


public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());

Get object information by converting to a string.
Object is the most common object method.


public class Student extends Object{
    private Integer id;
    private String name;

    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +


protected void finalize() throws Throwable { }

This method is used to release resources. Because it is impossible to determine when the method is called, it is rarely used.


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

Judge whether the contents are equal. Note that the memory address is not compared here.
The java language specification requires the equals method to have the following features:

  • Reflexivity: for any non null reference x,x.equals(x) should return true;
  • Symmetry: for any reference x, and y, if and only if y.equals(x) returns true,x.equals(y) should also return true;
  • Transitivity: for any reference x,y,z, if x.equals(y) returns true and y.equals (z) returns true, then x.equals(z) should also return true;
  • Consistency: if the objects referenced by X and y do not change, repeated calls to x.equals(y) should return the same result;
  • For any non null reference x,x.equals(null) returns false;


public native int hashCode();

This method is used for hash lookup. The equals method is rewritten. Generally, the hashCode method is rewritten. This method is used in some collections with hash function.
Object in Java has a method:

public native int hashcode();

(1) The role of the hashcode() method

The hashcode() method is mainly used with hash based collections, such as HashSet, HashMap, and HashTable.

When a new object needs to be added to the collection, first call the hashcode() method of the object to get the corresponding hashcode value. In fact, there will be a table in hashmap to save the hashcode value of the saved object. If the hashcode value is not changed in the table, it will be saved directly. If so, call the equals method to compare with the new element. The same will not be saved, but the different will be saved.

(2) Relationship between equals and hashcode

If equals is true, hashcode must be equal;

If equals is false, hashcode is not necessarily equal;

If hashcode values are equal, equals is not necessarily equal;

If hashcode values are different, equals must be different;

(3) When overriding the equals method, be sure to override the hashcode method


public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout value is negative");

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");

        if (nanos > 0) {


The wait method is to make the current thread wait for the lock of the object. The current thread must be the owner of the object, that is, it has the lock of the object. The wait() method waits until it obtains a lock or is interrupted. wait(long timeout) sets a timeout interval. If the lock is not obtained within the specified time, it returns.

After calling this method, the current thread goes to sleep until the following events occur.

  • Another thread called the notify method of the object.
  • The notifyAll method of the object was called by another thread.
  • Another thread called interrupt to interrupt the thread.
  • The time interval is up.

At this point, the thread can be scheduled. If it is interrupted, an InterruptedException exception will be thrown.


public final native void notify();

This method wakes up a thread waiting on the object.


public final native void notifyAll();

This method wakes up all threads waiting on the object.

Previous: Introduction to Java zero foundation 11: Java inheritance

Next: stay tuned

Add wechat, remark 1024, and give away the thinking map of Java learning route

Posted by blackswan on Sat, 20 Nov 2021 10:45:01 -0800