Java-Object

1
2
Class {@code Object} is the root of the class hierarchy. Every class has {@code Object} as a superclass. All objects, including arrays, implement the methods of this class.
类Object是类层次结构的根。 每个类都把Object作为父类。 所有对象,包括数组,实现该类的方法。

源码介绍如上。

1.hashcode()

1
2
@HotSpotIntrinsicCandidate
public native int hashCode();

native修饰的方法,不由java实现,hashcode()方法就是将该对象再内存中的位置转换为整数然后返回,所有的对象由JVM管理。

2.equals()

1
2
3
public boolean equals(Object obj) {
return (this == obj);
}

==即判断两者的内存地址是否相同。

对于String来说,其重写了equals()函数,先判断内存地址然后再判断内容。

需要注意的是必须保证如果两者的equals()为true,那么hashcode()相同。即重写的话也要先判断内存地址是否相同,然后对于不同的情况再判断其他的。

建议但不强制对于不相等的对象的hashCode一定要不同。

3.clone()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 
* The method {@code clone} for class {@code Object} performs a
* specific cloning operation. First, if the class of this object does
* not implement the interface {@code Cloneable}, then a
* {@code CloneNotSupportedException} is thrown. Note that all arrays
* are considered to implement the interface {@code Cloneable} and that
* the return type of the {@code clone} method of an array type {@code T[]}
* is {@code T[]} where T is any reference or primitive type.
* Otherwise, this method creates a new instance of the class of this
* object and initializes all its fields with exactly the contents of
* the corresponding fields of this object, as if by assignment; the
* contents of the fields are not themselves cloned. Thus, this method
* performs a "shallow copy" of this object, not a "deep copy" operation.
*/
@HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;

只包含了部分注释,这一部分注释解释了该函数的内容:

如果该类没有实现Cloneable接口,会抛出CloneNotSupportedException异常。

如果实现了Cloneable接口,那么该方法将会创建该类的新实例,并且初始化所有字段,字段的内容本身不是克隆的。 从而,这种方法执行此对象的“浅拷贝”,而不是“深拷贝”操作

4.toString()

1
2
3
4
//It is recommended that all subclasses override this method.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

返回类名+@+哈希值(内存地址转换而成)

5.wait(), notify(), notifyAll()

java的线程同步函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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++;
}
wait(timeoutMillis);
}

6.finalize()函数

1
2
@Deprecated(since="9")
protected void finalize() throws Throwable { }

finalize()方法就是再JVM确定已经没有任何线程可以访问该对象,那么将会清空该内存区域,如果其他类又使用了该类将会报异常。抛出的是Throwable,说明除了常规的异常Exceprion外,还有可能是JVM错误。当一个对象在回收前想要执行一些操作,就要覆写Object类中的finalize( )方法。

@Deprecated(since=”9”)表示自从JDK9已废除该方法