java源码——java.lang.Object

来源:互联网 发布:淘宝能微信支付吗 编辑:程序博客网 时间:2024/05/22 06:37

—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.——From Oracle


—释义—

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

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




—Object—

默认构造方法



—clone—



—equals—

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-nullobjectreferences:   —From ORacle—

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

源码:

 public boolean equals(Object obj) {       return (this == obj);       }   
通过源码我们看到,原始类equals其实与“==”是等价的。


—finalize—



—getClass—


—hashcode—

In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). This hash is used by other code when storing or manipulating the instance – the values are intended to be evenly distributed for varied inputs for use in clustering. This property is important to the performance of hash tables and other data structures that store objects in groups ("buckets") based on their computed hash values. Technically, in Java, hashCode() by default is a native method, meaning, it has the modifier 'native', as it is implemented directly in the native code in the JVM.

Source:Wikipedia

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

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

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

②、配合equals使用。


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



—notify—



—notifyall—



—toString—

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
源码:

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

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


—wait—





finalize()
0 0
原创粉丝点击