前言
之前一段时间. 曾经阅读了部分JDK 1.8 源码
.
首先阅读的是java.lang.Object
类.
详细内容
主要方法.
以上的几个方法可以分成几类:
- native 关键字方法.
- 非native关键字方法.
所谓native
关键字方法指的是调用的是计算机本地的方法. 比如Linux
内核, Windows
内核. 其对于方法的实现是完全不同的, 这也是Java为什么可以跨平台运行的原因.(根据本地内核, 调整项目内的底层接口方法.)
感兴趣的同学可以自己编译下JDK源码进行调试运行.
按照职责划分可以分为:
- 线程相关 (wait() / wait(long) / wait(long,nanos) / notify() / notifyAll())
- 启动流程相关 (registerNatives() / finalize())
- 其他( getClass() / hashCode() / equals() / clone() / toString() )
方法详解
-
getClass()
native
方法. 获取类的类型. 需要注意的是子类与父亲类的getClass()
方法. -
hashCode()
native
方法.hashCode()
方法. 获取对象的hash值. 在继承的子类中经常会重写此方法.
### String.class
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
-
clone()
native
方法. 拷贝创建. 这里的拷贝是浅拷贝
. 值得注意的是, 拷贝是一种设计模式中常见的原型模式. -
equals()
equals()
判断是否相等.
public boolean equals(Object obj) {
return (this == obj);
}
经常会问到的一个问题. equals
与==
有什么区别?
在Object
类中. equals
与==
没有区别, 都是比较对象的地址. 唯一的区别是equals
方法可以重写. 比如: 在Intger类中的重写见如下所示:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
此处首先使用instanceof
关键字比对两者数据类型是否相等. 随后比较2着的intValue
值.
toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
默认的toString()
方法是其name@(16进制HashCode)
.
- registerNatives()
加载native
相关模块. 感兴趣可以看下其OpenJDK
的实现.
private static native void registerNatives();
static {
registerNatives();
}
- finalize()
protected void finalize() throws Throwable { }
此处是空方法. 此方法在对象销毁时调用.
- wait() / wait(long) / wait(long,nanos)
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
线程等待. native
方法.
- notify() / notifyAll()
线程唤醒.native
方法.
public final native void notify();
public final native void notifyAll();
相关问题
-
equals 方法与
==
有什么区别? -
getClass() 获取的是子类还是父类? instanceof可以判断子类类型么?
Object obj = new Integer(1);
System.out.println(obj instanceof Integer);
System.out.println(obj.getClass());
//true
//class java.lang.Integer
- 浅拷贝与深拷贝.
源码
package java.lang;
public class Object {
private static native void registerNatives();
static {
registerNatives();
}
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object obj) {
return (this == obj);
}
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
protected void finalize() throws Throwable { }
}
Reference
[1]. JDK1.8源码(一)——java.lang.Object类
后记 2020-12-25
今天面试的时候,被问到了java.lang.Object
类有哪几个方法。我没怎么回答上来。这里重新记录一下回答技巧。
首先, 多线程相关。
- wait
- notify
拷贝相关
- clone
比较和输出
- equals
- toString
- hashCode
类相关
- getClass
其他
- registerNatives
- finalize