java学习笔记(1)-Object类
2017-08-07 15:59
267 查看
以下系列笔记都是基于jdk1.8
首先看下Object类的源码,其方法如下:
private static native void registerNatives();
static {
registerNatives();
}
public final native Class<?> getClass();
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
protected void finalize() throws Throwable { }
public boolean equals(Object obj) {
return (this == obj);
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
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);
}
【1】注册本地方法:registerNatives() 静态块中调用
【2】本地方法notify()、notifyall()、wait(long timeout)与多线程有关
【3】equals() 方法判断对象相等
【4】本地方法hashCode() 表示对象的地址,每次执行都不一定相等
如果equals相等,则hashCode必定相等;反之则不一定
如果重写了类的equals,必须要重写hashCode;反之亦然
【5】保护方法finalize() 与GC有关
首先看下Object类的源码,其方法如下:
private static native void registerNatives();
static {
registerNatives();
}
public final native Class<?> getClass();
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
protected void finalize() throws Throwable { }
public boolean equals(Object obj) {
return (this == obj);
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
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);
}
【1】注册本地方法:registerNatives() 静态块中调用
【2】本地方法notify()、notifyall()、wait(long timeout)与多线程有关
【3】equals() 方法判断对象相等
【4】本地方法hashCode() 表示对象的地址,每次执行都不一定相等
如果equals相等,则hashCode必定相等;反之则不一定
如果重写了类的equals,必须要重写hashCode;反之亦然
【5】保护方法finalize() 与GC有关
相关文章推荐
- java学习笔记6--类的继承、Object类
- Java学习笔记之一(深入理解Object类中方法)
- 多态Object类toString方法学习笔记ObjectDemo.java
- 黑马程序员--Java基础学习笔记【Object类、String类】
- j2se学习笔记-java.lang中的object类
- Java学习笔记五:Object类,equals ==,toString
- java学习笔记6--类的继承、Object类
- Java菜鸟学习笔记--面向对象篇(十六):Object类方法
- 【JAVA_SE学习笔记】Object类
- [置顶] JavaSE学习笔记_6:Java-Object类
- Java学习笔记二十四:Java中的Object类
- java学习笔记6--类的继承、Object类
- Java学习笔记---16.面向对象编程11-Java多态性,instanceof及Object类
- Java学习笔记(十九):Object类
- 【JAVA学习】EffectiveJava的学习笔记--第二章 Object类的通用方法
- (16)Java学习笔记——面向对象09——Object类
- java学习笔记6--类的继承、Object类
- java学习记录笔记--继承,super,Object类
- Java 学习笔记(0x09) object类
- java学习笔记-object类