Object class not ended

Posted by leeming on Sun, 27 Feb 2022 15:29:34 +0100

Object class is the base class of all classes in Java. It is the top of the whole class inheritance structure and the most abstract class.

Object contains 12 methods: registerNatives(), getClass(), hasCode(), equals(), clone(), toString(), notify(), notifyAll(), wait(long,int), wait(), finalize().

        registerNatives()

        getClass()

public final natove Class getClass();

The public method can be called directly through the object. The getClass method is a collection of all the information of the object of the current class at runtime.

        hashCode()

public native int hashCode();

public method subclass can override it. The hashCode method returns the hashCode value of the current object address by default. After rewriting, it is a user-defined calculation method. The value is (- 2 ^ 31 ~ 2 ^ 31-1) in the integer range.

import java.util.stream.Stream;

public class lengthannotation {
        private int age;
        private String name;
//Pay attention to the creation and use of constructors;
    lengthannotation(int age,String name){
        this.age=age;
        this.name=name;
    }
        public int hashCode () {
            //Stream.of() creates a sequential stream for the specified element;
            //Stream. The toArray () method returns an Object [] array;
            Object[] a = Stream.of(age, name).toArray();
            int result = 1;
            for (Object element : a) {
                result = 31 * result + (element == null ? 0 : element.hashCode());
            }
            return result;
        }
        public static void main(String[] args) {
            //Call hashCode() after creating the object;
            lengthannotation length=new lengthannotation(1,"Zhao Zhao");
            System.out.println(length.hashCode());
    }
}

        equals()

public boolean equals(Object obj);

It is used to compare whether the current object is equal to the target object. By default, it is used to compare whether the reference (object address) points to the same object. Subclasses can be overridden. After rewriting in the class, the rewritten equals method will be called.

Reason for rewriting: when defining an object array that does not allow duplicate values, the equals method judges the object address (Reference). No matter the value is different, the memory address of the new object must be different, so the equals method cannot judge and needs to be rewritten.

Note: when rewriting the equals method, the hashCode method should also be rewritten.

When equals() is not overridden:

public class Equalstest {
    public static void main(String[] args) {
        //Create a List of students to hold elements of type students
        List<Student> students = new ArrayList<>();
        //Declare the student object of student type. Note that it is declared and not created;
        Student student;
        for(int i=0;i<10;i++){
            //Create a student object, and each iteration will create an object;
            student=new Student("Xiao Ming");
            //Judge whether the student is included in the students list. If not, add it to the list
            //The contains() method determines whether the gift box contains an element. If yes, it returns true and if not, it returns false;
            //Note: the equals() method is used in the source code of contains()
            if(!students.contains(student)){
                students.add(student);
            }
        }
        System.out.println(students.size());//Output 10
    }
}
class Student{
    private String name;
    public Student(String name){
       this.name=name;
    }
}

When equals() is overridden:

public class Equalstest2 {
    public static void main(String[] args) {
        List<Student2> students=new ArrayList<>();
        Student2 student;
        for(int i=0;i<10;i++){
            student=new Student2("Xiao Ming");
            if(!students.contains(student)){
                students.add(student);
            }
        }
        System.out.println(students.size());//1
    }
}
class Student2{
    private String name;
    public Student2(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    //Override the equals method
    public boolean equals(Object obj){
        if(obj==this){
            return true;
        }
        if(obj instanceof Student2){
            Student2 student = (Student2) obj;
            if(student.getName().equals(this.name)){
                return true;
            }
        }
        return false;
    }
}

        clone()

protected native Object clone() throws CloneNotSupportedException;

The protected method is provided for subclass rewriting, but the clonable method needs to be implemented. This is a tag interface. If it is not implemented, when calling object CloneNotSupportedException will be thrown by clone() method.

Note: when using the clone() method, you must override the clone() method.

        toString()

public String toString();

public method. Subclasses can be overridden. The default toString method returns the fully qualified class name + @ + hexadecimal hashCode value of the current class.

How to rewrite: write according to what you want to return.

	public String toString() {
		return "[name = " + getName() + ",age = "+ getAge() + "]";
	}

        notify()

        notifyAll()

        wait(long)

        wait(long,int)

        wait()

These three methods are used for inter thread communication. Their function is to block the current thread and wait for other threads to call the notify()/notifyAll() method to wake it up. These three methods are public final and cannot be overridden.

        finalize()

Topics: Java Back-end