Object类是所有类,数组,枚举类的父类,当定义一个类时,没有使用extends关键字为它显式指定父类,则该类默认继承Object类
所有的Java类都是Object类的子类,所以任何Java对象 都可以调用Object类的方法。
Object类的几个常用方法:
boolean equals(Object obj):用来判断指定 对象时候与该对象相等。
public class Test {
public static void main(String[] args){
//创建Dog对象
Dog dog1 = new Dog();
Dog dog2 = new Dog();
Dog dog3 = dog1;
//下面将输出false,因为dog1和dog2指向堆内存中的两个对象
System.out.println(dog1.equals(dog2));
//下面将输出true,因为将dog1赋值给了dog3,所以两个对象引用指向堆内存中的同一个对象
System.out.println(dog1.equals(dog3));
}
}
class Dog{
public Dog() {
// TODO Auto-generated constructor stub
}
}
protected void finalize():当系统中没有引用变量引用到该对象时,系统垃圾回收器调用此方法来清理该对象的资源,java内存资源的回收是系统自动回收,不需要程序员来关注,如果显式调用了此方法,系统也不会立即清理该对象的资源,而是在合适的时候清理。
Class<?> getClass():此方法用于返回该对象的 运行时类
public class Test {
public static void main(String[] args){
Dog dog1 = new Dog();
//系统会输出 class com.learn.classLibraries.Dog
//内容包含包名
System.out.println(dog1.getClass());
}
}
class Dog{
public Dog() {
// TODO Auto-generated constructor stub
}
}
String toString():返回该对象的字符串表示,当程序使用System.out.println()方法输出一个对象或则把某个对象和字符串进行连接时,系统会自动调用toString()方法返回该对象的 字符串表示