初级Java开发工程师面试必备项之Java常用类:Object类
概述
- 它为超类、基类,位于继承数的最顶层,是所有类的直接或间接父类
- 任何类只要没有声明 extends 显示继承某个类,那么都会默认继承 Object 类,否则间接继承 Object 类
- Object 类中所定义的方法,是所有对象都具备的方法,所以子类可以使用 Object 的所有方法
所有方法:
修饰符和类型 | 方法及描述 |
---|---|
protected Object | clone() 创建并返回此对象的副本。 |
boolean | equals(Object obj) 指示一些其他对象是否等于此。 |
protected void | finalize() 当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。 |
Class<?> | getClass() 返回此 Object 的运行时类。 |
int | hashCode() 返回对象的哈希码值。 |
void | notify() 唤醒正在等待对象监视器的单个线程。 |
void | notifyAll() 唤醒正在等待对象监视器的所有线程。 |
String | toString() 返回对象的字符串表示形式。 |
void | wait() 导致当前线程等待,直到另一个线程调用该对象的 notify() 方法或 notifyAll() 方法。 |
void | wait(long timeout) 导致当前线程等待,直到另一个线程调用 notify() 方法或该对象的 notifyAll() 方法,或者指定的时间已过。 |
void | wait(long timeout, int nanos) 导致当前线程等待,直到另一个线程调用该对象的 notify() 方法或 notifyAll() 方法,或者某些其他线程中断当前线程,或一定量的实时时间。 |
示范类:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
getClass()方法
-
类型:
Class<?>
-
返回引用中储存的实际对象类型
-
作用:通常用于判断两个引用中实际存储对象类型是否一致
-
示例:
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//判断s1和s2是不是同个类型:getClass();
Class class1 = s1.getClass();
Class class2 = s2.getClass();
System.out.println(class1 == class2 ? "s1和s2属于同个类型" : "s1和s2不属于同个类型");
}
}
输出
s1和s2属于同个类型
hashCode()方法
-
类型:int
-
返回该对象的哈希码值
-
哈希值根据 对象的地址 或 字符串 或 数字 使用 hash 算法计算出来的 int 类型的数值
-
一般情况下相同的对象返回相同的哈希码值
-
示例:
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//返回s1和s2的哈希码值,不同对象返回的哈希码不一样
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
Student s3 = s1;
//s1地址赋给了s3,所以它们的哈希码值是一样的
System.out.println(s3.hashCode());
}
}
输出
2129789493
668386784
2129789493
toString()方法
-
类型:String
-
返回该对象的字符串表示形式
-
示例:
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//返回对象字符串表示形式:类的名字 + @ + 十六进制的哈希码值
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
输出
Student@7ef20235
Student@27d6c5e0
-
拓展:还可以通过在类中重写 toString() 方法,返回自己自定义的字符串
-
示例:
重写 Student 类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//返回对象字符串表示形式
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
输出
Student{name='lisa', age=20}
Student{name='kirot', age=21}
equals()方法
-
类型:boolean
-
默认实现为(this == obj),比较两个对象地址是否相同
-
可进行覆盖,比较两个对象的内容是否相同
-
示例:
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//equals();判断两个对象是否相等
System.out.println(s1.equals(s2));
Student s3 = s1; //s1地址赋给s3
System.out.println(s1.equals(s3));
Student s4 = new Student("w",18);
Student s5 = new Student("w",18);
System.out.println(s4.equals(s5)); //s4和s5地址不一样
}
}
输出
false
true
false
equals()方法的覆盖步骤
-
比较两个引用是否指向同一个对象
-
判断 obj 是否为 null
-
判断两个引用指向的实际对象类型是否一致
-
强制类型转换
-
依次比较各个属性值是否相同
-
示例
重写 equals() 方法
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
//1.判断两个对象是否是同一个引用
if (this == obj) {
return true;
}
//2.判断obj是否为null
if (obj == null) {
return false;
}
//3.判断是否是同个类型
if (obj instanceof Student) { //instanceof判断对象是否是某种类型
//4.强制类型转换
Student s = (Student) obj;
//5.比较属性
if (this.name.equals(s.getName()) && this.age == s.getAge()) {
return true;
}
}
return false;
}
}
```
```c
public static void main(String[] args) {
Student s1 = new Student("lisa",20);
Student s2 = new Student("kirot", 21);
//equals();判断两个对象是否相等
System.out.println(s1.equals(s2));
Student s3 = s1; //s1地址赋给s3
System.out.println(s1.equals(s3));
Student s4 = new Student("w",18);
Student s5 = new Student("w",18);
System.out.println(s4.equals(s5));
}
输出
false
true
true
finalize()方法(已弃用)
- 当对象被判定为垃圾对象时,由 JVM 自动调用此方法,用以标记垃圾对象,进入回收队列
- 垃圾对象:没有有效引用指向此对象时,为垃圾对象
- 垃圾回收:由 GC 销毁垃圾对象,释放数据存储空间
- 自动回收机制:JVM 的内存耗尽,一次性回收所有垃圾对象
- 手动回收机制:使用
System.gc();
通知 JVM 执行垃圾回收
clone()方法
-
创建并返回此对象的拷贝
-
clone()
是浅拷贝,对象内属性引用的对象只会拷贝引用地址,而不会将引用的对象重新分配内存,相对应的深拷贝则会连引用的对象也重新创建 -
由于 Object 本身没有实现 Cloneable 接口,所以不重写 clone 方法并且进行调用的话会发生 CloneNotSupportedException 异常
-
示例:
public class Student implements Cloneable{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Student s1 = new Student("dt", 20);
System.out.println(s1.getName()+"\t"+s1.getAge());
try {
//创建s1的拷贝
Student s2 = (Student) s1.clone();
System.out.println(s2.getName()+"\t"+s2.getAge());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
//加入Java开发交流君样:593142328一起吹水聊天
}
}
输出
dt 20
dt 20