天天看点

Java 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.

该类位于java.lang(java语言核心包)包下。这个类有一个无参的构造方法以及若干个常用方法protected Object clone()、boolean equals()、protected void finalize()、Class<?>getClass()、int hashCode()、void notify()、void notifyAll()、String toString()、void wait()、void wait(long timeout)、void wait(long timeout,int nanos)

因为每个类都继承自该类,所以所有的类都具有这些方法。

方法详解

public String 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())

Object类的toString方法返回一个字符串,这个字符串由实例对象的名称[email protected]+该对象的无符号的十六进制哈希码

注意:java语言的8种基本数据类型没有toString()方法,但是他们的包装类具有该方法,而且Object的子类都重写了该方法

public int hashCode()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by 

HashMap

.

关于hashcode,官方文档给了三个说明: 1、同一个对象在程序运行期间的任何时候返回的哈希值都是始终不变的 2、如果两个对象根据equals方法是相等的,则调用这两个对象中任一对象的hashCode方法必须产生相同的整数结果 3、如果两个对象的hashCode相同,它们并不一定相同(equals)

public boolean equals(Object obj)      

请参考另一篇博客详解Object类的equals方法

public final Class<?> getClass()      

请参照另一篇博客 JAVA反射入门

protected Object clone()throws CloneNotSupportedException      
请参照详解Java中的clone方法 -- 原型模式      
protected void finalize()throws Throwable      

其余的几个是和线程相关的方法

参考文献:Java总结篇系列:java.lang.Object

                  Java提高篇——对象克隆(复制)