一、概述
1、介绍
Object 类属于 java.lang 包,此包下的所有类在使用时无需手动导入,系统会在程序编译期间自动导入。
Object 类是所有类的基类,如果一个类没有使用 extends 标识继承另外一个类,那么这个类默认继承Object类。任何类都直接或间接继承此类。
类结构图:
代码示例:Object类源码
1 public class Object { 2 3 private static native void registerNatives(); 4 static { 5 registerNatives(); 6 } 7 8 public final native Class<?> getClass(); 9 10 public native int hashCode(); 11 12 public boolean equals(Object obj) { 13 return (this == obj); 14 } 15 16 protected native Object clone() throws CloneNotSupportedException; 17 18 public String toString() { 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 timeout) throws InterruptedException; 27 28 public final void wait(long timeout, int nanos) throws InterruptedException { 29 if (timeout < 0) { 30 throw new IllegalArgumentException("timeout value is negative"); 31 } 32 33 if (nanos < 0 || nanos > 999999) { 34 throw new IllegalArgumentException( 35 "nanosecond timeout value out of range"); 36 } 37 38 if (nanos > 0) { 39 timeout++; 40 } 41 42 wait(timeout); 43 } 44 45 public final void wait() throws InterruptedException { 46 wait(0); 47 } 48 49 protected void finalize() throws Throwable { } 50 }