The java.lang.Object class of common Java APIs (with analysis and examples) _02

It is very necessary to understand Object. The Object class is the root class in Java , that is, other classes in Java use the Object class as a superclass.

All objects (including arrays) implement the methods of this class, and the methods in the Object class can be used on any object.

----------------------------------------Common methods--------- ---------------------------------------

Common methods of Object
protected  Object clone()  Creates and returns a copy of this object. By default, shallow clone is used. If subclasses need deep clone, this method can be overridden.
boolean equals(Object obj) Determine whether some other object obj is equal to this object.
Class<?> getClass()  Returns the runtime class of this Object.
 int hashCode()  Returns the hash code value for this object.
 String toString()   Returns a string representation of this object.


(1)protected  Object  clone( ) 

protected native Object clone() throws CloneNotSupportedException

It can be seen from the source code of the Object class that the Object class itself does not implement the interface Cloneable, so calling the clone method on an object whose class is Object will cause an exception to be thrown at runtime. Object's clone() method uses a shallow clone. If the subclass needs deep cloning, it needs to implement the cloneable interface and override the clone( ) method.

The copy method of shallow clone is: copy one copy of the basic data type in memory, and use the method of reference for other types, so there is a security risk in shallow clone.

The clone() method of the Object class is a native method, and the efficiency of the native method is generally much higher than that of the non-native method in Java . This also explains why to use the clone() method in Object instead of creating a new class and then copying the information from the original object to the new object. The Object.clone() method returns an Object object. We have to cast to get the type we need.



(2)boolean  equals(Object obj)

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

As can be seen from the source code, the default is to call the == operator to compare the addresses of two objects, not the value of the objects. If the subclass needs to judge whether the objects are equal by the value of the two objects, it needs to override this method in the subclass. When this method is overridden, it is often necessary to override the hashCode method to maintain the hashCode method's general contract, which states that equal objects must have equal hash codes. We compare two objects most of the time. At this time, the equals() method of Object cannot meet the requirements. In fact, in the JDK, encapsulation classes such as String and Math have rewritten the equals() method.

(3)Class<?> getClass( ) 

// source code
public final native Class<?> getClass();

public class Test //instance
{
	public static void main(String[] args)
	{
		String str= new String();  
		System.out.println(str.getClass());  
	}
}
// output
class java.lang.String
(4)int hashCode( ) 
public native int hashCode();

The value calculated by the hashCode() method locally implemented by Object is the implementation of the underlying code. It adopts a variety of calculation parameters, and the returned value is not necessarily the (virtual) memory address of the object, depending on the specific implementation of the runtime library and JVM.

Basic rules for overriding hashCode() method:

    Multiple calls to the hashCode method on the same object must consistently return the same integer during the execution of a Java application.
    If two objects are equal according to the equals(Object) method, then calling the hashCode method on both objects must produce the same integer result.


(5)String  toString() 

public String toString()//Source code
{
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
The toString method of the Object class returns a string consisting of the class name (the object is an instance of that class), the at marker "@", and the unsigned hexadecimal representation of this object's hash code, not A concise but easy-to-read informational expression. It is recommended that all subclasses override this method.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325705859&siteId=291194637