JDK8源码阅读笔记--------java.lang.Object

Object:

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

是所有类的超类。

1.getClass():

Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

2.hashCode():

Returns a hash code value for the object.

3.equals(Object obj):

4.clone():

扫描二维码关注公众号,回复: 4858707 查看本文章

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object.

意思是:准确的说克隆的意思是,得到类的class和原来类的class是一样的,如下: 

public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        //false
        System.out.println(arrayList == arrayList.clone());
        //true
        System.out.println(arrayList.getClass() == arrayList.clone().getClass());
        //true
        System.out.println(arrayList.clone().equals(arrayList));
    }
5.toString():
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
推荐所有子类重写这个方法。
默认返回:getClass().getName() + "@" + Integer.toHexString(hashCode());
public static void main(String[] args) {
        Object o = new Object();
        //java.lang.Object@4554617c
        System.out.println(o.toString());
    }

6.notify():

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.

7.notifyAll():

Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

8.wait()、wait(long timeout):

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

9.finalize():

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

意思是当GC确定这个对象没有任何引用时候,由gc调用这个方法。子类重写finalize()方法来中断系统资源或者做清理。

猜你喜欢

转载自blog.csdn.net/weixin_39035120/article/details/84318358