Object和Objects的区别

Object类

如果一个类没有指定父类,默认就是继承Object类。
Object类里面共有11个方法:
经常用到的equals(),toString()都是直接使用或者重写的Object里面的这些方法;
另外还有final native修饰的方法:反射需要用到的getClass(),线程操作需要的notify(),notifyAll(),wait(long timeout);
还有native修饰的方法:hashCode()和clone()

返回值 方法名 作用
protected Object clone() 创建并返回此对象的副本
boolean equals(Object obj) 判断一些其他对象是否等于此
protected void finalize() 当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象
Class<?> getClass() 返回此Object的运行时类
int hashCode() 返回对象的哈希码值
void notify() 唤醒正在等待对象监视器的单个线程
void notifyAll() 唤醒正在等待对象监视器的所有线程
String toString() 返回对象的字符串表示形式
void wait() 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法
void wait(long timeout) 导致当前线程等待,直到另一个线程调用 notify()方法或该对象的 notifyAll()方法,或者指定的时间已过
void wait(long timeout, int nanos) 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法,或者某些其他线程中断当前线程,或一定量的实时时间

Object类

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 { }
}

Objects类

Objects类是final修饰的类,不可继承,内部方法都是static方法,从jdk1.7开始才引入了Objects类

返回值 方法名 作用
static int compare(T a, T b, Comparator<? super T> c) 如果参数a,b相同则返回0,否则返回c.compare(a, b)的结果
static boolean deepEquals(Object a, Object b) 对比a,b参数是否深层次相等,假定a,b为数组,对比数组的每个参数
static boolean equals(Object a, Object b) 对比a,b参数是否相等
static int hash(Object… values) 为输入值序列生成哈希码
static int hashCode(Object o) 返回哈希码。如果o为null则返回0
static boolean isNull(Object obj) 如果obj参数为null返回true
static boolean nonNull(Object obj) 如果obj参数不为null返回true
static T requireNonNull(T obj) 检查指定的对象引用不是 null,为null报空指针错误
static T requireNonNull(T obj, String message) 检查指定的对象引用不是null并抛出自定义的NullPointerException(如果是)
static T requireNonNull(T obj, Supplier messageSupplier) 检查指定的对象引用不是null并抛出一个自定义的NullPointerException(如果是)
static String toString(Object o) 返回对象的字符串表示形式
static String toString(Object o, String nullDefault) 如果第一个参数不是 null ,则返回第一个参数调用 toString的结果,否则直接将第二个参数nullDefault返回

Objects类

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package java.util;

import java.util.function.Supplier;

public final class Objects {
private Objects() {
throw new AssertionError("No java.util.Objects instances for you!");
}

public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}

public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}

public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}

public static int hash(Object... values) {
return Arrays.hashCode(values);
}

public static String toString(Object o) {
return String.valueOf(o);
}

public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}

public static <T> int compare(T a, T b, Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}

public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}

public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}

public static boolean isNull(Object obj) {
return obj == null;
}

public static boolean nonNull(Object obj) {
return obj != null;
}

public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
if (obj == null)
throw new NullPointerException(messageSupplier.get());
return obj;
}
}