在学习容器类的过程中,发现不少知识点,是需要理解Object之后,才能掌握的,于是就去JDK看了一下Object。
Object是Java中所有类的鼻祖,在类继承树的根部位置上。于是所有的类,都算是Object的子类。于是所有的类,都实现了Object的方法。
作为the root of the class hierarchy,Object自然是有点高冷的,所以它实现的方法比较少。
- clone( )创造并且返回一个object的副本
- equals( )用来判断两个object是否相等
- finalize( )当确定不再调用一个object时,就拔屌无情,垃圾回收机制(garbage collector)就会调用此方法
- hashcode( )哈希哈希,该方法返回object的散列码
- notify( )和notifyall( ) notify英语是通知的意思,这来个函数用来通知/唤醒在object的监控器上的线程
- toString( ) 返回一个代表该object的字符串,一般是类的名字加这个类的散列码
- 三个wait( )方法,让目前的的线程进入等待状态,与notify( )功能相对
.
.
.
public String toString( )
显然要返回一个字符串,这个字符串含有这个object的一些信息。相当于调用了代码
getClass().getName() + ‘@’ + Integer.toHexString(hashCode()) ,于是可知toString方法会返回这个类的名字@散列码。
.
.
.
public boolean equals(Object obj)
覆盖equals函数需要满足5个条件:
It is reflexive: for any non-null reference value x, x.equals(x) should return true. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false.