JavaSE--Object类中一些方法:toString、equal、finalize、hashCode

发布时间 2023-08-15 10:53:35作者: 洛小依ovo

1、toString方法

// 源码
public String toString() {
    return this.getClass().getName() + "@" + Integer.toHexString(hashCode());
}

 toString方法一般都是要重写的,是要越简洁越好,可读性越强越好。向简洁的、详实的、易阅读的方向发展

public class Test01{
	public static void main(String[] args){
		MyTime t1 = new MyTime(1970, 1, 1);
		// 一个日期对象转换成字符串形式的话,我可能还是希望能看到具体的日期信息。
		String s1 = t1.toString();

		//MyTime类重写toString()方法之前
		//System.out.println(s1); // MyTime@28a418fc
		
		//MyTime类重写toString()方法之后
		System.out.println(s1); // 1970年1月1日

		// 注意:输出引用的时候,会自动调用该引用的toString()方法。
		System.out.println(t1);
	}
}
class MyTime{
	int year;
	int month;
	int day;

	public MyTime(){
	
	}

	public MyTime(int year, int month, int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}

	// 重写toString()方法
	// 越简洁越好,可读性越强越好。
	// 向简洁的、详实的、易阅读的方向发展
	public String toString(){
		//return this.year + "年" + this.month + "月" + this.day + "日";
		return this.year + "/" + this.month + "/" + this.day;
	}
}

 2、equal方法

  • 判断基本数据类型是否相等,直接使用==就可以
  • 判断java对象是否相等,需要使用重写equals方法

    因为Object中的equal方法内部使用的双等号

// equals方法源码
public boolean equals(Object obj) {
    return (this == obj);
}
public class Test02{
	public static void main(String[] args){

		// 判断两个基本数据类型的数据是否相等直接使用“==”就行。
		int a = 100;
		int b = 100;
		// 这个“==”是判断a中保存的100和b中保存的100是否相等。
		System.out.println(a == b); //true(相等) false(不相等)

		MyTime t1 = new MyTime(2008, 8, 8); //MyTime t1 = 0x1234;
		// 创建了一个新的日期对象,但表示的日期也是:2008年8月8日。
		MyTime t2 = new MyTime(2008, 8, 8); //MyTime t2 = 0x3698;
		
		System.out.println(t1 == t2); // false

		// 重写Object equals方法之后(比较的是内容。)
		boolean flag = t1.equals(t2);
		System.out.println(flag); //true

		// 再创建一个新的日期
		MyTime t3 = new MyTime(2008, 8, 9);
		// 两个日期不相等,就是false。
		System.out.println(t1.equals(t3)); // false

	}
}

class MyTime {
	int year;
	int month;
	int day;

	public MyTime(){
	
	}
	public MyTime(int year, int month, int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}

    // 自己重写的equal方法
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof MyTime)){
			return false;
		}
		if(this == obj){
			return true;
		}
		MyTime t = (MyTime)obj;
		return this.year == t.year && this.month == t.month && this.day == t.day ;
	}

}

 3、String类中重写的equals方法

  String类中自己重写了equal方法,可以直接使用

4、finallize方法

// 在Object类中的finalize源代码:
protected void finalize() throws Throwable { }

 1)GC垃圾回收器:负责调用finalize()方法。程序员不需要手动调用

  2)finalize()方法的执行时机:
            当一个java对象即将被垃圾回收器回收的时候,垃圾回收器负责调用finalize()方法

  3)finalize()方法实际上是SUN公司为java程序员准备的一个时机,垃圾销毁时机。如果希望在对象销毁时机执行一段代码的话,这段代码要写到finalize()方法当中。

  4)java中的垃圾回收器不是轻易启动的,垃圾太少,或者时间没到,种种条件下,有可能启动,也有可能不启动。

public class Test06{
	public static void main(String[] args){
		/*
		// 创建对象
		Person p = new Person();

		// 怎么把Person对象变成垃圾?
		p = null;
		*/
		for(int i = 0; i < 1000; i++){
			Person p = new Person();
			p = null;

			// 可以建议垃圾回收器启动
			System.gc(); // 建议启动垃圾回收器。(只是建议,可能不启动,也可能启动。启动的概率高了一些。)
		}		

	}
}
class Person{
	// 重写finalize()方法
	// Person类型的对象被垃圾回收器回收的时候,垃圾回收器负责调用:p.finalize();
	protected void finalize() throws Throwable {
		System.out.println(this + "即将被销毁!");
	}

}

 

5、hashCode方法

  hashCode()方法返回的是哈希码:实际上就是一个java对象的内存地址,经过哈希算法,得出的一个值

  所以hashCode()方法的执行结果可以等同看做一个java对象的内存地址

public native int hashCode();
// 这个方法不是抽象方法,带有native关键字,底层调用C++程序。
public class Test07{
	public static void main(String[] args){
		Object o = new Object();
		int hashCodeValue = o.hashCode();

		// 对象内存地址经过哈希算法转换的一个数字。可以等同看做内存地址。
		System.out.println(hashCodeValue); //798154985

		MyClass mc = new MyClass();
		int hashCodeValue2 = mc.hashCode();
		System.out.println(hashCodeValue2); //1392838296

		MyClass mc2 = new MyClass();
		System.out.println(mc2.hashCode()); // 523429285
	}
}

class MyClass{
}