java object 详解_Java记录 -22- Java的基类Object详解

Java的基类Object详解

Java的JDK文档要经常查阅使用,最好查看英文的文档。

Oracle官方在线 Java API Specifications

java.lang.Object 类,java.lang包在使用的时候无需显示导入,编译时由编译器自动帮助我们导入。

API(Application Programinga Interface),应用编程接口。

Object obj = new Object();

System.out.println(obj);

System.out.println(obj.toString());

上面打印的两个内容一样。当打印引用时,实际上会打印出引用所指向对象的 toString()方法的返回值,因为每个类都直接或间接的继承自Object,而Object类中定义了 toString(),因此每个类都有 toString()这个方法。

String str = "aaa";

System.out.println(str);

System.out.println(str.toString());

两个输出内容都为 aaa,按照上面的理解,Sting一定重写了Object的 toString()方法。

当打印一个引用对象时,会显示例如:java.lang.Object@c17164。为什么会打印这么一个串呢?我们通过查看Object类的toString()方法就可以知道。public String toString()

Returns a string representation of the object. In general, the toString method returns

a string that "textually represents" this object. The result should be a concise

but informative representation that is easy for a person to read. It is recommended

that all subclasses override this method.

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

Returns:

a string representation of the object.

java.lang.Object@c17164    类名+@+地址的十六进制表示形式

6. 关于进制的表示,16进制,逢16进一,16进制的数字包括:0~9,A,B,C,D,E,F

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值