java basic_Object class

java basic_Object class

The Object class is the ancestor of all classes in java, and any class inherits object by default. Even if there is no direct inheritance, there will eventually be indirect inheritance. The methods in the Object class are common to all subclasses.

toString() method

1. toString() method source code

public String toString() {
    
    
	// 返回的是一个: 类名@对象的内存地址转换为十六进制的形式
	return this.getClass().getName() + "@" + Integer.toHexString(hashCode());
}

2. What is the function of the toString() method?
The design purpose of the toString() method is: by calling this method, a "java object" can be converted into a "string representation".

3. In fact, when SUN developed the java language, it is recommended that all subclasses rewrite the toString() method. The toString() method should be concise, informative, and easy to read.

Note: When System.out.println("reference") prints the object reference, it will automatically call the reference's toString() method.

equals() method

1. equals() method source code

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

2. What is the purpose of SUN's design of the equals method?
In the process of programming in the future, the equals method must be used to judge whether two objects are equal. The equals method is to determine whether two objects are equal.

3. The default equals method provided by the Object class is not enough.
Among the equals methods in the Object class, == is used by default to determine whether two java objects are equal. And == judges whether the memory addresses of two java objects are equal, we should judge whether the contents of two java objects are equal. Therefore, the ancestor's equals method is not enough, and subclasses need to rewrite equals.

4. The difference between == and equals
== is an operator. If it is a basic data type, it compares the stored value; if it is a reference data type, it compares the memory address of the pointed object.
equals is a method of the Object class, and the default comparison is the memory address of the pointed object. In general, what is compared after rewriting is (the value of the member variable of the object) the content of the object.

5. Rewrite the equals() method and pay attention to the downward transformation.

To rewrite a method, it needs to have the same return value type, the same method name, and the same formal parameter list.

public boolean equals(Object obj) {
    
    
	// 如果obj是null,或者obj不是一个Penson类
	// 不能直接 用 this.id与obj.id比较,因为Object类中没有id、name..等属性。
	if(obj == null || !(obj instanceof Penson) ){
    
    
		return false;
	}
	// 如果this和obj保存的内存地址相同,没必要比较了,返回true。
	if(this == obj){
    
    
		return true;
	}
	// 能执行到这里说明obj不是null是一个Penson类。
	Penson penson =(Penson)obj;  // 访问子类特有的变量,需要向下转型。
	// 程序执行到这,返回true表示相等,false表示不相等。
	return this.id == penson.id && this.name == penson.name && ...;
}

The String class rewrites the toString() method and the equals() method.

finalize() method

About the finalize() method in the Object class. JDK9+ is obsolete.

1. The source code in the Object class:

protected void finalize() throws Throwable {
    
     }

2. The finalize() method has only one method body, there is no code in it, and this method is protected.

3. This method does not need to be called manually by the programmer, and the garbage collector (GC) of the JVM is responsible for calling this method. Unlike equals toString, the equals and toString() methods require you to write code to call them. finalize() only needs to be rewritten, and the program will automatically call it in the future after rewriting.

4. The execution timing of the finalize() method:
When a java object is about to be reclaimed by the garbage collector, the garbage collector is responsible for calling the finalize() method.

5. The finalize() method is actually an opportunity prepared by SUN for java programmers, an opportunity for garbage destruction. If you want to execute a piece of code when the object is destroyed, this code should be written in the finalize() method.

6. What is the function of the static code block?
static { ... } The static code block is executed at the moment of class loading, and only executed once. This is a classloading opportunity prepared by SUN. The finalize() method is also an opportunity prepared by SUN for programmers. This timing is garbage collection timing.


7. Tips:
The garbage collector in java is not easy to start. There is too little garbage, or the time is not up. Under various conditions, it may or may not start.

hashCode() method

hashCode() method source code:

public native int hashCode();

This method is not an abstract method, with the native keyword, and the bottom layer calls the C++ program.

The hashCode() method returns a hash code:
in fact, it is the memory address of a java object, which is a value obtained through a hash algorithm. Therefore, the execution result of the hashCode() method can be regarded as the memory address of a java object.

Guess you like

Origin blog.csdn.net/hu4545/article/details/125566796