目录
1. 源码
public class Object {
private static native void registerNatives();
static {
registerNatives();
}
@HotSpotIntrinsicCandidate
public Object() {}
@HotSpotIntrinsicCandidate
public final native Class<?> getClass();
@HotSpotIntrinsicCandidate
public native int hashCode();
public boolean equals(Object obj) {
return (this == obj);
}
@HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
@HotSpotIntrinsicCandidate
public final native void notify();
@HotSpotIntrinsicCandidate
public final native void notifyAll();
public final void wait() throws InterruptedException {
wait(0L);
}
public final native void wait(long timeoutMillis) throws InterruptedException;
public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("timeoutMillis value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
timeoutMillis++;
}
wait(timeoutMillis);
}
@Deprecated(since="9")
protected void finalize() throws Throwable { }
}
2. 涉及关键词
2.1 Native
Java中Native关键字的作用
调用其他语言的关键字。例如海康威视的SDK
为C语言编译之后的DLL
文件,但是提供的有JAVA
使用方案,就是采用Native
(JNI)实现的。
HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary(dllPath + "HCNetSDK.dll", HCNetSDK.class);
通过以上方法返回一个接口的实例化对象,通过编写接口提供和dll
相同的方法和参数实现调用dll
文件的方法.
2.2 @HotSpotIntrinsicCandidate
网上都说Java 9引入的新特性,JDK的源码中,被@HotSpotIntrinsicCandidate标注的方法,在HotSpot中都有一套高效的实现,该高效实现基于CPU指令,运行时,HotSpot维护的高效实现会替代JDK的源码实现,从而获得更高的效率。但本人未找到具体的处理方法,在此挖个坑
2.3 @Deprecated(since=“9”)
表示此方法已废弃、暂时可用,但以后此类或方法都不会再更新、后期可能会删除,建议后来人不要调用此方法。
- since: 从什么时候开始不建议使用的,一般标注为版本号
- forRemoval:是否会移除,默认为不会移除。
3. 方法解析
3.1 registerNatives()
调用其他语言方法关联到Object上
3.2 getClass()
获取该对象的类型,是内存中实际对象类型,而非父类或者子类的类型。
3.3 hashCode()
获取该对象的哈希码,采用的其他语言的实现方式。
3.4 equals(Object obj)
3.5 clone()
3.6 toString()
3.7 notify()
3.8 notifyAll()
3.9 wait()
3.10 wait(long timeoutMillis)
3.11 wait(long timeoutMillis, int nanos)
3.12 finalize()
4.阅读文档
- Java根类Object的方法说明
- Java语言中Object对象的hashCode()取值的底层算法是怎样实现的
- hashcode方法