首先介绍JDK中所有类的基类——java.lang.Object。
Object 类属于 java.lang 包,此包下的所有类在使用时无需手动导入,系统会在程序编译期间自动导入。Object 类是所有类的基类,当一个类没有直接继承某个类时,默认继承Object类,也就是说任何类都直接或间接继承此类,Object 类中能访问的方法在所有类中都可以调用,下面我们会分别介绍Object 类中的所有方法。
1、Object 类的结构图
Object.class类
1 /* 2 * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 */ 6 7 package java.lang; 8 9 /** 10 * Class {@code Object} is the root of the class hierarchy. 11 * Every class has {@code Object} as a superclass. All objects, 12 * including arrays, implement the methods of this class. 13 * 14 * @author unascribed 15 * @see java.lang.Class 16 * @since JDK1.0 17 */ 18 public class Object { 19 20 private static native void registerNatives(); 21 static { 22 registerNatives(); 23 } 24 25 public final native Class<?> getClass(); 26 27 public native int hashCode(); 28 29 public boolean equals(Object obj) { 30 return (this == obj); 31 } 32 33 protected native Object clone() throws CloneNotSupportedException; 34 35 public String toString() { 36 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 37 } 38 39 public final native void notify(); 40 41 public final native void notifyAll(); 42 43 public final native void wait(long timeout) throws InterruptedException; 44 45 public final void wait(long timeout, int nanos) throws InterruptedException { 46 if (timeout < 0) { 47 throw new IllegalArgumentException("timeout value is negative"); 48 } 49 if (nanos < 0 || nanos > 999999) { 50 throw new IllegalArgumentException( 51 "nanosecond timeout value out of range"); 52 } 53 if (nanos > 0) { 54 timeout++; 55 } 56 wait(timeout); 57 } 58 59 public final void wait() throws InterruptedException { 60 wait(0); 61 } 62 63 protected void finalize() throws Throwable { } 64 }