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方法返回一个字符串,这个字符串由实例对象的名称+@+该对象的无符号的十六进制哈希码

注意: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提高篇——对象克隆(复制)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值