您的位置:首页 > 移动开发 > Objective-C

Java记录 -22- Java的基类Object详解

2015-09-03 18:34 561 查看
Java的基类Object详解
Java的JDK文档要经常查阅使用,最好查看英文的文档。
Oracle官方在线 Java API Specifications
http://www.oracle.com/technetwork/java/api-141528.html


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: