1.1 Object类源码阅读笔记(java8)

在正式开始阅读这个Object类之前,其实之前已经看过源码了,只是一直不知道用什么方式来呈现出我看源码的方式,所以在浏览了不少资料之后,才打算开始写,好了,废话不多说。直接上源码。

* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author  unascribed
* @see     java.lang.Class
* @since   JDK1.0
*/
public class Object {

这里的注释我们可以看到,这个Object类是所有类的最上层(root),每一个类的最顶层的类都是Object这个类,包括数组。

private static native void registerNatives();
    static {
        registerNatives();
    }

然后接着往下看,我们可以看到,这个方法使用了native,代表这是一个自然方法,也就是jvm中本地的方法,我们知道java使用c++写的,这里面就是使用了JNI,在java代码里,将外部的c++的方法使用JNI的方式注入到class文件中。在静态代码块中,在编译期的时候就会自动执行了,代表注册本地方法。

接着往下看,我们可以看到一个同样是本地方法的getClass()


image.png

该方法也是一个native方法,同时他是一个静态,表示返回对象的运行时类,返回的对象是由所表示的类的方法所锁定的对象。总结就是返回运行时类名。

public native int hashCode();

hashCode这个方法,同样也是一个native方法,该方法能返回一个int类型的hash值,通常是将对象内部的地址转换为整数来实现,涉及到对地址分配的操作一般使用c++来操作的,所以也是一个native方法。如果两个对象的equal方法的结果相等,那么两个对象的hashCode的返回值也一定相等。

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

这个方法是用于判断本对象与传入的另外一个对象是否相等的。该方法声明的结果为true,那么两个对象的hashCode执行的int结果也是相等的。

    /**
     * Creates and returns a copy of this object.  The precise meaning
     * of "copy" may depend on the class of the object. The general
     * intent is that, for any object {@code x}, the expression:
     * <blockquote>
     * <pre>
     * x.clone() != x</pre></blockquote>
     * will be true, and that the expression:
     * <blockquote>
     * <pre>
     * x.clone().getClass() == x.getClass()</pre></blockquote>
     * will be {@code true}, but these are not absolute requirements.
     * While it is typically the case that:
     * <blockquote>
     * <pre>
     * x.clone().equals(x)</pre></blockquote>
     * will be {@code true}, this is not an absolute requirement.
     * <p>
     * By convention, the returned object should be obtained by calling
     * {@code super.clone}.  If a class and all of its superclasses (except
     * {@code Object}) obey this convention, it will be the case that
     * {@code x.clone().getClass() == x.getClass()}.
     * <p>
     * By convention, the object returned by this method should be independent
     * of this object (which is being cloned).  To achieve this independence,
     * it may be necessary to modify one or more fields of the object returned
     * by {@code super.clone} before returning it.  Typically, this means
     * copying any mutable objects that comprise the internal "deep structure"
     * of the object being cloned and replacing the references to these
     * objects with references to the copies.  If a class contains only
     * primitive fields or references to immutable objects, then it is usually
     * the case that no fields in the object returned by {@code super.clone}
     * need to be modified.
     * <p>
     * The method {@code clone} for class {@code Object} performs a
     * specific cloning operation. First, if the class of this object does
     * not implement the interface {@code Cloneable}, then a
     * {@code CloneNotSupportedException} is thrown. Note that all arrays
     * are considered to implement the interface {@code Cloneable} and that
     * the return type of the {@code clone} method of an array type {@code T[]}
     * is {@code T[]} where T is any reference or primitive type.
     * Otherwise, this method creates a new instance of the class of this
     * object and initializes all its fields with exactly the contents of
     * the corresponding fields of this object, as if by assignment; the
     * contents of the fields are not themselves cloned. Thus, this method
     * performs a "shallow copy" of this object, not a "deep copy" operation.
     * <p>
     * The class {@code Object} does not itself implement the interface
     * {@code Cloneable}, so calling the {@code clone} method on an object
     * whose class is {@code Object} will result in throwing an
     * exception at run time.
     *
     * @return     a clone of this instance.
     * @throws  CloneNotSupportedException  if the object's class does not
     *               support the {@code Cloneable} interface. Subclasses
     *               that override the {@code clone} method can also
     *               throw this exception to indicate that an instance cannot
     *               be cloned.
     * @see java.lang.Cloneable
     */
protected native Object clone() throws CloneNotSupportedException;

首先,我们可以看到,这个方法是一个protected的范围,说明只有继承该类的或者说该类的子类才可以调用。从注解来看,这是一个克隆方法,并且是“浅复制”的。按照惯例,一般是要该方法来克隆一个相同的属性的对象。
并且有以下的几种情况: 如果

  1. x.clone() != x 结果为true,那么表示x.clone().getClass() == x.getClass()为true
  2. x.clone().equals(x) 为true,这并不是绝对的。

如果对象的类不支持{@code Cloneable}接口。覆盖{@code clone}方法的子类也可以抛出此异常以指示无法克隆实例。并且需要注意,所有的1数组都有实现{@code Cloneable}接口。

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

返回对象的字符串表示形式。通常,* {@code toString}方法返回一个“文本表示”此对象的字符串。结果应该是一个简洁但信息丰富的表示,便于人阅读。 建议所有子类都覆盖此方法。该字符串由对象为实例的类的名称组成。at符号字符为`{@code @} ',对象的哈希码的无符号十六进制表示。换句话说,此方法返回一个等于*值的字符串。
toString()是由对象的类型和其哈希码唯一确定,同一类型但不相等的两个对象分别调用toString()方法返回的结果可能相同。

public final native void notify();

对java多线程有一定使用的,都知道这是唤醒正在等待此对象的单个线程。如果任何线程正在等待此对象,则选择其中一个被唤醒。与之相对应的是:

public final native void notifyAll();

他会唤醒所有等待此对象的所有等待线程。

有唤醒就会有等待状态:

public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
public final void wait() throws InterruptedException {
        wait(0);
    }

根据注释,可知,调用这个方法,会导致当前对象等待,知道另外一个线程为此对象调用notify或者notifyAll,或者经过了指定的时间,当前线程必须拥有此对象的监视器。

protected void finalize() throws Throwable { }

当垃圾收集*确定没有对该对象的更多引用时,由对象上的垃圾收集器调用。 子类重写{@code finalize}方法以处置系统资源或执行其他清理。
这个方法用于当对象被回收时调用,默认该类是不做任何调用,这个是对象逃离被JVM干掉的最后的机会,但是任何对象的这个方法只会被调用一次,在《深入理解JVM》一书中提出该函数的调用不确定性大、代价大不建议使用。

最后推荐一篇文章,还没时间深入去看,不过写的很详细的。汗颜////
https://blog.csdn.net/zhxdick/article/details/56673610

感谢您阅读我的文章,如果满意可以帮我点歌赞,谢谢哈。
如果对文章部分还有什么见解或者疑惑,可以私信评论我,欢迎技术讨论。如果需要获取完整的文件资源,可以加我微信z985085305,获取我整理的全套笔记。
思想的碰撞最能促进技术的进步哦。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,492评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,048评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,927评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,293评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,309评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,024评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,638评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,546评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,073评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,188评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,321评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,998评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,678评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,186评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,303评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,663评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,330评论 2 358

推荐阅读更多精彩内容