Java中的Object類有着特殊的意義,他是全部其它類的父類,查看Object類的源代碼,能夠發現代碼很少,邏輯也很簡單。java
Java全部類的源代碼咱們均可以在JDK的文件中查看,在JDK下會有一個名爲src.zip的壓縮包,裏面就是全部類的源代碼,固然這裏全部的文件都是.java格式的。爲了方便查看,咱們能夠找到JDK中jre/lib下的rt.jar文件,這裏面也有源代碼,只不過是以.class文件形式存在,咱們能夠使用反編譯軟件jd-gui(http://jd.benow.ca可下載)查看,會比直接查看.java文件方便很多。固然看源代碼以前先看看API,也能事半功倍。ui
如下是jdk1.7.0_40下Object類的源代碼this
1 public class Object 2 { 3 private static native void registerNatives(); 4 5 public final native Class<?> getClass(); 6 7 public native int hashCode(); 8 9 public boolean equals(Object paramObject) 10 { 11 return this == paramObject; 12 } 13 14 protected native Object clone() 15 throws CloneNotSupportedException; 16 17 public String toString() 18 { 19 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 20 } 21 22 public final native void notify(); 23 24 public final native void notifyAll(); 25 26 public final native void wait(long paramLong) 27 throws InterruptedException; 28 29 public final void wait(long paramLong, int paramInt) 30 throws InterruptedException 31 { 32 if (paramLong < 0L) { 33 throw new IllegalArgumentException("timeout value is negative"); 34 } 35 36 if ((paramInt < 0) || (paramInt > 999999)) { 37 throw new IllegalArgumentException("nanosecond timeout value out of range"); 38 } 39 40 if ((paramInt >= 500000) || ((paramInt != 0) && (paramLong == 0L))) { 41 paramLong += 1L; 42 } 43 44 wait(paramLong); 45 } 46 47 public final void wait() 48 throws InterruptedException 49 { 50 wait(0L); 51 } 52 53 protected void finalize() 54 throws Throwable 55 { 56 } 57 58 static 59 { 60 registerNatives(); 61 } 62 }
在Object類中,咱們常常會打交道方法主要是toString()、equals(Object paramObject)和hashCode()三個方法。spa
其餘的方法中,notify()、notifyAll()和wait(long paramLong)都是本地方法,和線程相關。還有兩個重載的方法wait()和wait(long paramLong, int paramInt)也都和線程相關。線程
finalize()是GC相關的方法。code