Object源码解读
Object类的方法
private static native void registerNatives();
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object obj) {
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
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 {
public final void wait() throws InterruptedException {
protected void finalize() throws Throwable { }
native关键字代表这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL,由Java去调用。native是用做Java和其他语言进行协作时用的,也就是native后的函数的实现不是用Java写的。
Objects源码解读
Objects中的方法都是static的,不需要new对象,可以直接调用。
final类型类
public final class Objects {}
final来修饰class,表明该类不可被继承。
私有构造方法
private Objects() {}
私有的Objects()构造方法,表明不能new对象。
equals()方法
public static boolean equals(Object a, Object b) {}
该方法比较数值是否相等,比较对象时,对象的引用只有在指向同一块内存区域时才相等。
deepEquals()方法
public static boolean deepEquals(Object a, Object b) {}
该方法比较数值是否相等,比较对象或数组等类型时,只要内容完全相等,则为相等。
hashCode()方法
public static int hashCode(Object o) {}
当传入的参数不为null时,返回相应的hashCode值,否则返回0;
hash()方法
public static int hash(Object... values) {}
调用Arrays.hashCode()方法。
toString()方法
public static String toString(Object o) {}
public static String toString(Object o, String nullDefault) {}
第一个方法调用String的valueOf方法来实现转换,第二个方法实现如果o不为空,则实现将该对象转换为字符串,否则默认返回为nullDefault。
compare()方法
public static <T> int compare(T a, T b, Comparator<? super T> c) {
该方法
requireNonNull()方法
public static <T> T requireNonNull(T obj) {}
public static <T> T requireNonNull(T obj, String message) {}
第一个方法判断对象是否为null,为null则抛出异常,否则返回该对象。
第二个方法判断对象是否为null ,为null则在抛出异常信息时,将message内容抛出,否则返回该对象。
isnull()方法
public static boolean isNull(Object obj) {
判断对象是够为null。返回obj==null。
nonNull()方法
public static boolean nonNull(Object obj) {
判断对象是否不为null,返回 obj != nul。