Java所有类都继承与Object,本文谈谈我对object源码的理解,如果有错的,请看官多多批评指正。

  1、registerNatives()

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

  注册本地方法,静态块内运行,将object内native方法都加载上,native修饰表示这个方法涉及JNI,注册本地方法,用C++写的程序。

  2、getClass()

 //@return The {@code Class} object that represents the runtime
class of this object.
public final native Class<?> getClass();

   native方法,调用此方法,返回运行时类名

  3、hashCode 

    / 
  *<li>Whenever it is invoked on the same object more than once during
*     an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code hashCode} method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hash tables.
  * @return  a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();

  native方法,返回对象的hashCode码。hashCode返回的并不一定是对象的内存地址,具体取决于运行时库和JVM中方法具体C++实现。

  • 如果两个对象object.equal()相等,那么他们的hashCode一定相等。
  • 如果两个对象通过object.equal()比较,结果不相等,这两个对象分别调用hashCode可以返回两个不相同的整数。
  • 如果对象没有重写equals(),或者重写equals中比较的属性没有被改变,则调用多次hashCode(),其返回值一定是同一个数值,不会变。

  4、equals()

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

  从上面看出来,Object的equals()比较的是对象的地址

  5、clone()

     / * @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;

  native()方法,表示对对象的深复制。

  如: Object A = new Object();       Object B = A;表示浅复制A,此处产生了一个对象A;

     Object C = new Object();       Object D = C.clone() 或者 Object D = new Object;表示深复制C,此处产生了两个对象C和D; 

  6、toString()

    / * @return  a string representation of the object.*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

  返回类名+16进制的hashCode

  7、notify()、notifyAll()、多用于同步代码块、同步方法中。用于唤醒wait()的线程。

  

public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
} if (nanos > 0) {
timeout++;
} wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}

  wait()线程需要先抛出中断异常,wait(long timeout, int nanos)timout毫秒单位,nanos纳秒,但是在JVM中只判断有值时,timeout加一毫秒,有点搞笑!

  wait(0)表示一直wait(),直至被notify。

  8、finalize()

 protected void finalize() throws Throwable { }

    垃圾回收器在认为该对象是垃圾对象的时候会调用该方法。子类可以通过重写该方法来达到资源释放的目的。 在方法调用过程中出现的异常会被忽略且方法调用会被终止。

任何对象的该方法只会被调用一次

  总结,Object类中,equal()、toString()、finalize()可以被重写,equal()重写时一般都会对hashCode修改。

jdk之object源码理解的更多相关文章

  1. java中Object源码理解

    java阅读笔记 1.object getClass() 返回是的此object运行时的类,返回的对象是被object锁定的对象,调用这个方法不需要进行强转 public static void ma ...

  2. 深入剖析(JDK)ArrayQueue源码

    深入剖析(JDK)ArrayQueue源码 前言 在本篇文章当中主要给大家介绍一个比较简单的JDK为我们提供的容器ArrayQueue,这个容器主要是用数组实现的一个单向队列,整体的结构相对其他容器来 ...

  3. Caffe源码理解2:SyncedMemory CPU和GPU间的数据同步

    目录 写在前面 成员变量的含义及作用 构造与析构 内存同步管理 参考 博客:blog.shinelee.me | 博客园 | CSDN 写在前面 在Caffe源码理解1中介绍了Blob类,其中的数据成 ...

  4. 基于SpringBoot的Environment源码理解实现分散配置

    前提 org.springframework.core.env.Environment是当前应用运行环境的公开接口,主要包括应用程序运行环境的两个关键方面:配置文件(profiles)和属性.Envi ...

  5. jedis的源码理解-基础篇

    [jedis的源码理解-基础篇][http://my.oschina.net/u/944165/blog/127998] (关注实现关键功能的类)   基于jedis 2.2.0-SNAPSHOT   ...

  6. .NET Core 3.0之深入源码理解Startup的注册及运行

    原文:.NET Core 3.0之深入源码理解Startup的注册及运行   写在前面 开发.NET Core应用,直接映入眼帘的就是Startup类和Program类,它们是.NET Core应用程 ...

  7. 深入源码理解Spring整合MyBatis原理

    写在前面 聊一聊MyBatis的核心概念.Spring相关的核心内容,主要结合源码理解Spring是如何整合MyBatis的.(结合右侧目录了解吧) MyBatis相关核心概念粗略回顾 SqlSess ...

  8. HashMap源码理解一下?

    HashMap 是一个散列桶(本质是数组+链表),散列桶就是数据结构里面的散列表,每个数组元素是一个Node节点,该节点又链接着多个节点形成一个链表,故一个数组元素 = 一个链表,利用了数组线性查找和 ...

  9. VUEJS2.0源码理解--优

    VUEJS2.0源码理解 http://jiongks.name/blog/vue-code-review/#pingback-112428

随机推荐

  1. [转载]系统管理:update-alternatives

    http://blog.csdn.net/dbigbear/article/details/4398961 好吧,其实博主也是转载的. update-alternatives --display | ...

  2. bzoj 4206 最大团 几何+lis

    最大团 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 142  Solved: 65[Submit][Status][Discuss] Descrip ...

  3. 不可不知的robots.txt文件

    robots.txt基本介绍 robots.txt是一个纯文本文件,在这个文件中网站管理者可以声明该网站中不想被robots访问的部分,或者指定搜索引擎只收录指定的内容. 当一个搜索机器人(有的叫搜索 ...

  4. telnet退出

    windows下退出telnet:可以参考下面linux退出,也可以直接关闭窗口. linux退出telnet: 1.输入ctrl+]:显示出telnet>. 2.此时可以输入?,查看可以使用的 ...

  5. 详解ListView加载网络图片的优化

    我们来了解一些ListView在加载大量网络图片的时候存在的常见问题: 1.性能问题,ListView的滑动有卡顿,不流畅,造成非常糟糕的用户体验. 2.图片的错位问题. 3.图片太大,加载Bitma ...

  6. Debug模式下加载文件,运行程序异常的慢

    今天在进行单元测试的时候,debug模式下加载速度很慢,但是run模式下速度很快. 原因:在debug模式下,断点位置不当,解决办法 移除编译器中的所有断点.

  7. js for等循环 跳出多层循环

    js for 循环 跳出多层循环 ,,,,,,,]; // 8个数 ,,,,,,,]; //8个数 testFor(); console.log(') function testFor() { ;k& ...

  8. 「模板」「讲解」Treap名次树

    Treap实现名次树 前言 学平衡树的过程可以说是相当艰难.浏览Blog的过程中看到大量指针版平衡树,不擅长指针操作的我已经接近崩溃.于是,我想着一定要写一篇非指针实现的Treap的Blog. 具体如 ...

  9. centos7.2编译安装zabbix-3.0.4

    安装zabbix-3.0.4 #安装必备的包 yum -y install gcc* make php php-gd php-mysql php-bcmath php-mbstring php-xml ...

  10. 知问前端——cookie插件

    Cookie是网站用来在客户端保存识别用户的一种小文件.一般可以保存用户登录信息.购物数据信息等一系列微小信息. 一.使用cookie插件 官方网站:http://plugins.jquery.com ...