Object類
Object類概述
類 Object 是類層次結構的根類。每個類都使用 Object 作爲超類。所有對象(包括數組)都實現這個類的方法。所有的類都直接或者間接的繼承自Object類。該類的設計也符合面向對象中“萬事萬物皆對象”的思想。
構造方法 :
public Object()
任何一個類都會調用這個方法,訪問子類構造方法的首先會先訪問父類無參的構造方法。
常用方法
1.int hashCode()
返回該對象的哈希碼值。
2.Class<?> getClass()
獲取類的路徑
3.oolean equals(Object obj)
比較兩個對象的地址
4.String toString()
返回的是 類路徑 + @ + 對象的hashCode的十六進制轉換
5.protected Object clone()
克隆對象
6.protected void finalize()
當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。
方法解析
1.int hashCode()
返回該對象的哈希碼值。
返回該對象的哈希碼值。支持此方法是爲了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能。
特點
1.該方法會被哈希表結構利用,哈希表結構會利用這個方法來計算哈希索引值,
從而儘量避免哈希衝突,此方法大部分需要子類重寫,此方法的重寫是爲了提高哈希表的性能 【看完集合的哈希表數據結構以及HashMap底層源碼實現後再回過頭來看】
2.實際上,由 Object 類定義的 hashCode 方法確實會針對不同的對象返回不同的整數。
(這一般是通過將該對象的內部地址轉換成一個整數來實現的,但是 JavaTM 編程語言不需要這種實現技巧。)
3.兩個對象如果hashCode相同,不能夠完全保證連個對象相同,如果兩個對象的hashCode不同,那麼兩個對象肯定不同
A 0x0001 B 0x0002
A hashCode 12345 B hashCode 12345
4. == 和 equals方法的區別
比較基本數據類型
== 比較的是數值
equals 不可以,這是一個方法
引用數據類型
== 比較的是真實地址
equals 暫時保留
equals 比較 只能夠比較引用類型,默認比較的地址,如果重寫了一般比較的是內容,具體比較的結果看需求。
代碼示例
public class ObjectDemo01 {
public static void main(String[] args) {
Student s1 = new Student("張三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("張三", 18);
Student s4 = s1;
System.out.println(s1.hashCode()); // 2018699554
System.out.println(s2.hashCode()); // 2018699554
System.out.println(s3.hashCode()); // 118352462
System.out.println(s4.hashCode()); // 2018699554
int a = 10;
int b = 10;
System.out.println(a == b);
System.out.println(s1 == s2);
}
}
2.Class<?> getClass()
萬事萬物皆對象,利用多個對象的共性來定義 類, 類是用來描述現實世界的事物的,Java當中class來定義類
類用來描述事物,類本身也是一種概念事物,所以Java使用一個類Class來描述 概念事物
class Class {
成員變量對象
成員方法對象
構造方法對象
訪問權限修飾符對象
…
}
代碼示例
public class ObjectDemo02 {
public static void main(String[] args) {
Student s1 = new Student("張三", 18);
Student s2 = new Student("李四", 20);
Teacher t = new Teacher();
Class c1 = s1.getClass();
Class c2 = s2.getClass();
Class c3 = t.getClass();
System.out.println(c1);
System.out.println(c1 == c2); // true
System.out.println(c1 == c3); // false
String className = c1.getName();
System.out.println(className); // com.sxt.objectdemo.Student
String className2 = c2.getName();
System.out.println(className2); // com.sxt.objectdemo.Student
System.out.println(t.getClass().getName());
}
}
class Teacher {}
3.oolean equals(Object obj)
該方法默認比較的是連個對象的地址值是否相等。
觀察equals的源碼:
public boolean equals(Object obj) {
return (this == obj); // s1 == s3
}
比較兩個對象比的是地址沒有意義,一般判斷兩個對象是否相等,比較的是對象的成員,所以我們一般自定義對象都需要重寫equals方法
如何重寫? – 學習String的equals方法
參照String重寫equals方法的規則
但是實際開發中一般是自動生成,但是有的時候還是要根據需求來的,通過Eclipse工具快捷鍵: alt + shift + S 再按H
代碼示例
public class ObjectDemo04 {
public static void main(String[] args) {
Student s1 = new Student("張三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("李四", 20);
Student s5 = new Student(null, 22);
Student s6 = new Student(null, 22);
Student s4 = s1;
System.out.println(s1.equals(s2)); // false
System.out.println(s2.equals(s3)); // true
System.out.println(s1.equals(s4)); // true
// String s = new String("abc");
// String ss = new String("abc");
// System.out.println(s.equals(ss));
System.out.println(s1.equals(new Teacher()));
System.out.println(s1.equals(null));
System.out.println(s5.equals(s6));
UUID randomUUID = UUID.randomUUID();
System.out.println(randomUUID); // 1d0d1b83-1848-479b-b82d-2d3e94288b19
}
}
4.String toString()
輸出一個對象默認輸出該引用類型的toString方法
觀察toString在Object類中的源碼
public String toString() {
return this.getClass().getName() + “@” + Integer.toHexString(this.hashCode());
}
默認toString返回的是 類路徑 + @ + hashCode值的十六進制
這個字符串對我們來說沒有意義,父類Object類中的toString方法不能夠滿足子類Student的需求,所以需要方法重寫
如何來重寫? – 一般都是打印對象的成員值
如果一個類中有100個成員,我們都一個一個拼接成員嗎? 可以通過Eclipse工具自動生成即可 快捷鍵: alt + shift + s 再按S
代碼示例
public class ObjectDemo03 {
public static void main(String[] args) {
Student s1 = new Student("張三", 18);
Student s2 = new Student("李四", 20);
System.out.println(s1.hashCode()); // 2018699554
String str1 = s1.toString();
String str2 = s2.toString();
System.out.println(str1); // com.sxt.objectdemo.Student@7852e922
System.out.println(str2); // com.sxt.objectdemo.Student@4e25154f
System.out.println(s1.toString());
System.out.println(s1);
Student s3 = null;
System.out.println(s3);
}
}
5.protected Object clone()
該方法表示對對象進行克隆
注意事項:
1.clone方法是protected修飾,子類需要重寫,外界才能訪問
2.clone的對象必須支持克隆,需要克隆的對象必須實現Cloneable接口
3.多個引用指向同一個堆區空間的問題和克隆的區別
4.深拷貝和淺拷貝
代碼示例
public class ObjectDemo05 {
public static void main(String[] args) throws CloneNotSupportedException {
Student s = new Student("張三", 25);
Object obj = s.clone();
// System.out.println(obj);
Student sClone = (Student) obj;
sClone.setName("張三的影子");
sClone.setAge(30);
System.out.println(s);
System.out.println(sClone);
}
}
6.protected void finalize()
當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。
子類重寫 finalize 方法,以配置系統資源或執行其他清除。
代碼示例
public class ObjectDemo06 {
public static void main(String[] args) {
Student s = new Student();
s = null;
// 啓動垃圾回收器
System.gc();
// 通知系統的垃圾回收期對象去調用垃圾回收期重寫的finalize方法
System.runFinalization();
}
}