Object类常用方法
object 是 Integer 的父类?
在 Java 中,Object
类是所有类的超类。这意味着如果一个类没有明确地继承其他类,那么它默认继承Object
类
class MyClass{
// 这个类默认继承了Object类
}
toString()
方法
- 该方法主要用于返回一个对象的字符串表示形式。
- 默认情况下,它返回的是对象的类名和一个哈希码(十六进制表示)
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice", 25);
System.out.println(p);
}
}
equals()
方法
用途:用于比较两个对象是否相等。在Object
类中,equals()
方法默认是比较两个对象的引用是否相同,也就是判断两个对象是否是同一个对象。
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass()!= o.getClass()) return false;
Book book = (Book) o;
return title.equals(book.title) && author.equals(book.author);
}
}
public class Main {
public static void main(String[] args) {
Book book1 = new Book("Java核心技术", "Cay Horstmann");
Book book2 = new Book("Java核心技术", "Cay Horstmann");
System.out.println(book1.equals(book2));
}
}
hashCode()
方法
- 用途:返回对象的哈希值。在
Object
类中,hashCode()
方法返回的是一个基于对象内存地址的整数。 - 规则:如果两个对象根据
equals()
方法比较是相等的,那么它们的hashCode()
值必须相同;但是如果两个对象的hashCode()
值相同,它们不一定相等(这就是所谓的哈希冲突)。
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 重写hashCode方法(简单示例,实际应用可能更复杂)
@Override
public int hashCode() {
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + age;
return result;
}
// 重写equals方法,很重要,因为hashCode相等时,equals通常也要进行合适判断
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass()!= o.getClass()) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);
}
}
public class HashCodeDemo {
public static void main(String[] args) {
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Alice", 25);
System.out.println("person1的hashCode: " + person1.hashCode());
System.out.println("person2的hashCode: " + person2.hashCode());
System.out.println("person1和person2是否相等(通过equals判断): " + person1.equals(person2));
}
}
clone()
方法
用途:用于创建并返回一个对象的副本。
浅拷贝
class Person implements Cloneable {
private String name;
private int age;
private Address address;
public Person(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
// 重写clone方法实现浅克隆
@Override
public Person clone() throws CloneNotSupportedException {
return (Person) super.clone();
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Address getAddress() {
return address;
}
}
class Address {
private String city;
public Address(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
public class ShallowCloneDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Beijing");
Person person1 = new Person("Alice", 25, address);
Person person2 = person1.clone();
System.out.println("person1的信息:");
System.out.println("姓名: " + person1.getName());
System.out.println("年龄: " + person1.getAge());
System.out.println("地址: " + person1.getAddress().getCity());
System.out.println("person2的信息:");
System.out.println("姓名: " + person2.getName());
System.out.println("年龄: " + person2.getAge());
System.out.println("地址: " + person2.getAddress().getCity());
// 修改person2中地址对象的城市信息
person2.getAddress().setCity("Shanghai");
System.out.println("修改person2地址后:");
System.out.println("person1的地址: " + person1.getAddress().getCity());
System.out.println("person2的地址: " + person2.getAddress().getCity());
}
}
深拷贝
import java.io.*;
class Person implements Cloneable, Serializable {
private String name;
private int age;
private Address address;
public Person(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
// 重写clone方法实现深克隆(利用序列化和反序列化)
@Override
public Person clone() throws CloneNotSupportedException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (Person) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new CloneNotSupportedException(e.getMessage());
}
}
public String getName() {
return name;
}
public String getAge() {
return String.valueOf(age);
}
public Address getAddress() {
return address;
}
}
class Address implements Serializable {
private String city;
public Address(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
public class DeepCloneDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Beijing");
Person person1 = new Person("Alice", 25, address);
Person person2 = person1.clone();
System.out.println("person1的信息:");
System.out.println("姓名: " + person1.getName());
System.out.println("年龄: " + person1.getAge());
System.out.println("地址: " + person1.getAddress().getCity());
System.out.println("person2的信息:");
System.out.println("姓名: " + person2.getName());
System.out.println("年龄: " + person2.getAge());
System.out.println("地址: " + person2.getAddress().getCity());
// 修改person2中地址对象的城市信息
person2.getAddress().setCity("Shanghai");
System.out.println("修改person2地址后:");
System.out.println("person1的地址: " + person1.getAddress().getCity());
System.out.println("person2的地址: " + person2.getAddress().getCity());
}
}
finalize()
方法
用途:在对象被垃圾回收之前调用,用于执行一些清理操作。
如:对象中使用了一些外部资源,如文件句柄、网络连接、数据库连接等,在对象不再被使用时,需要在 finalize() 方法中释放这些资源,以防止资源泄漏。
class MyObject {
// 重写finalize方法
@Override
protected void finalize() throws Throwable {
System.out.println("对象 " + this + " 即将被回收,正在执行finalize方法");
super.finalize();
}
}
public class FinalizeDemo {
public static void main(String[] args) {
MyObject obj = new MyObject();
// 将obj置为null,让其可以被垃圾回收
obj = null;
// 主动触发垃圾回收(注意,垃圾回收的实际执行时机仍不确定,这只是建议虚拟机执行回收)
System.gc();
try {
// 让主线程休眠一会,给垃圾回收线程机会去执行finalize方法(只是增加执行的可能性)
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}