服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java源码阅读之java.lang.Object

java源码阅读之java.lang.Object

2021-03-14 13:41Leesire Java教程

这篇文章主要介绍了java源码阅读之java.lang.Object,具有一定借鉴价值,需要的朋友可以参考下

object是所有类的父类,任何类都默认继承object。object类到底实现了哪些方法?

1.clone方法

保护方法,实现对象的浅复制,只有实现了cloneable接口才可以调用该方法,否则抛出clonenotsupportedexception异常。

2.getclass方法

final方法,获得运行时类型。

3.tostring方法

该方法用得比较多,一般子类都有覆盖。

4.finalize方法

该方法用于释放资源。因为无法确定该方法什么时候被调用,很少使用。

5.equals方法

该方法是非常重要的一个方法。一般equals和==是不一样的,但是在object中两者是一样的。子类一般都要重写这个方法。

6.hashcode方法

该方法用于哈希查找,重写了equals方法一般都要重写hashcode方法。这个方法在一些具有哈希功能的collection中用到。

一般必须满足obj1.equals(obj2)==true。可以推出obj1.hash-code()==obj2.hashcode(),但是hashcode相等不一定就满足equals。不过为了提高效率,应该尽量使上面两个条件接近等价。

7.wait方法

wait方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait()方法一直等待,直到获得锁或者被中断。wait(longtimeout)设定一个超时间隔,如果在规定时间内没有获得锁就返回。

调用该方法后当前线程进入睡眠状态,直到以下事件发生。

(1)其他线程调用了该对象的notify方法。

(2)其他线程调用了该对象的notifyall方法。

(3)其他线程调用了interrupt中断该线程。

(4)时间间隔到了。

此时该线程就可以被调度了,如果是被中断的话就抛出一个interruptedexception异常。

8.notify方法

该方法唤醒在该对象上等待的某个线程。

9.notifyall方法

该方法唤醒在该对象上等待的所有线程。

—object—

classobjectistherootoftheclasshierarchy.everyclasshasobjectasasuperclass.allobjects,includingarrays,implementthemethodsofthisclass.——fromoracle

—释义—

object类是java中所有对象所继承的父类,即便是数组也继承了该父类(可以理解为原始类,所有类的祖先,你也许会想问:詹姆斯第一个写的类是不是object?)。

所有类对object类的继承都是隐式继承,所以无法看到。

java源码阅读之java.lang.Object

—object—

默认构造方法

—clone—

—equals—

indicateswhethersomeotherobjectis"equalto"thisone.

theequalsmethodimplementsanequivalencerelationonnon-nullobjectreferences:—fromoracle—

原始类object的equals比较的是两个变量的非空对象的引用。

源码

?
1
2
3
public boolean equals(object obj) {  
  return (this == obj);  
  }

通过源码我们看到,原始类equals其实与“==”是等价的。

—finalize—

—getclass—

—hashcode—

inthejavaprogramminglanguage,everyclassimplicitlyorexplicitlyprovidesahashcode()method,whichdigeststhedatastoredinaninstanceoftheclassintoasinglehashvalue(a32-bitsignedinteger).thishashisusedbyothercodewhenstoringormanipulatingtheinstance–thevaluesareintendedtobeevenlydistributedforvariedinputsforuseinclustering.thispropertyisimportanttotheperformanceofhashtablesandotherdatastructuresthatstoreobjectsingroups("buckets")basedontheircomputedhashvalues.technically,injava,hashcode()bydefaultisanativemethod,meaning,ithasthemodifier'native',asitisimplementeddirectlyinthenativecodeinthejvm.

source:wikipedia

java中每个类都隐式或者显式的实现了object的hashcode方法。

跟谷歌和官方个人总结,作者为什么要在原始类中存在hashcode呢?

①、类对象的存储优化,便于查找类对象。

②、配合equals使用。

注意:很多博客表示hashcode方法返回的是该类的物理存储地址或者是逻辑存储地址,这个说法是错误的,按照官方的说法:返回的32位值只是与类对象的存储位置有关。

—notify—

—notifyall—

—tostring—

thetostringmethodforclassobjectreturnsastringconsistingofthenameoftheclassofwhichtheobjectisaninstance,theat-signcharacter`@',andtheunsignedhexadecimalrepresentationofthehashcodeoftheobject.inotherwords,thismethodreturnsastringequaltothevalueof:

getclass().getname()+'@'+integer.tohexstring(hashcode())

源码:

?
1
2
3
public string tostring() { 
   return getclass().getname() + "@" + integer.tohexstring(hashcode()); 
 }

返回一个格式为类名+@+该类的hash值。

—wait—

finalize()

总结

以上就是本文关于java源码阅读之java.lang.object的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/lee_sire/article/details/53466444

延伸 · 阅读

精彩推荐