java.lang.Object是所有类的基类。
native修饰的属于本地方法,底层使用C/C++实现。
一、常见的方法
1、public final native Class<?> getClass();
返回类的Class对象。
获取Class对象的三种方式:
1、实例对象的getClass()方法。如:test.getClass();
2、java中所有的类型都有class属性(包括基本数据类型、enum枚举类型)。如:Test.class; int.class;
3、Class类的类方法:Class.forName(全限类名);
2、public native int hashCode();
根据对象的地址或者字符串或者数字算出来的int类型的数值。
不同的对象hashCode()有可能放回相同的值。
3、 equals方法。默认情况下(像String等类型重写了equals()和hashCode()的除外)对象之间的比较,equals等价于 ==。
public boolean equals(Object obj) {
return (this == obj);
}
hashCode()与equals()的关系?
1、如果两个对象equals为true,则它们的hashCode()返回值一定相同。
2、如果两个对象hashCode返回值相同,不能代表两个对象相同。只能代表在散列存储结构中,存放的位置是相同的。
3、一般两者应该同时重写。
4、 protected native Object clone() throws CloneNotSupportedException;
复制对象。需要该类实现java.lang.Cloneable接口,否则抛出异常CloneNotSupportedException。
浅复制和深复制:
浅复制:clone方法复制时,对象的属性值的复制是通过复制引用的方式完成对象的复制,原对象与clone对象的引用类型属性属于同一个,任何一个的修改都会影响另外一个。
1、默认情况下,调用clone()是浅复制。
2、要实现深复制,一般需要重写clone方法。
5、toString()方法。
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode()); //类名@16进制的hashCode
}
6、 public final native void notify();
任意唤醒一个等待该对象的线程。Wakes up a single thread that is waiting on this object’s monitor.
7、public final native void notifyAll();
唤醒所有等待该对象的线程。Wakes up all threads that are waiting on this object’s monitor.
8、 使一个线程进入阻塞状态,直到它被唤醒。 Causes the current thread to wait until it is awakened.
public final void wait() throws InterruptedException {
wait(0L);
}
public final native void wait(long timeoutMillis) throws InterruptedException;