目录
- 1 Objects
- 1.1 Objects方法
- 1.2 Objects常用方法
- 1.2.1 equals(Object a, Object b)
- 1.2.2 isNull(Object obj)
- 1.2.3 nonNull(Object obj)
- 1.2.4 requireNonNull(T obj)
- 1.3 Objects源码
- 2 区别于Object
- 2.1 Object构造方法
- 2.2 Object其他方法
- 2.2.1 equals(Object obj)
- 2.2.2 toString()
- 2.3 Object源码
- 总结
1 Objects
1.1 Objects方法
工具类,常用于检查操作
1.2 Objects常用方法
1.2.1 equals(Object a, Object b)
equals(Object a, Object b)源码:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
1.2.2 isNull(Object obj)
isNull(Object obj)源码:
public static boolean isNull(Object obj) {
return obj == null;
}
1.2.3 nonNull(Object obj)
nonNull(Object obj)源码:
public static boolean nonNull(Object obj) {
return obj != null;
}
1.2.4 requireNonNull(T obj)
requireNonNull(T obj)源码:
public static T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
1.3 Objects源码
package java.util; import jdk.internal.util.Preconditions; import jdk.internal.vm.annotation.ForceInline; 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 requireNonNullElse(T obj, T defaultObj) { return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj"); } public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) { return (obj != null) ? obj : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()"); } public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) { if (obj == null) throw new NullPointerException(messageSupplier == null ? null : messageSupplier.get()); return obj; } @ForceInline public static int checkIndex(int index, int length) { return Preconditions.checkIndex(index, length, null); } public static int checkFromToIndex(int fromIndex, int toIndex, int length) { return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null); } public static int checkFromIndexSize(int fromIndex, int size, int length) { return Preconditions.checkFromIndexSize(fromIndex, size, length, null); } }
2 区别于Object
2.1 Object构造方法
public Object()
- Object类是基类,是所有类的父类(基类)
- 如果一个类没有明确的继承某一个具体的类,则将默认继承Object类
例如我们定义一个类:
public class Person{
}
其实它被使用时 是这样的:
public class Person extends Object{
}
- object的多态:使用Object可以接收任意的引用数据类型
例:
public static void main(String[] args){ String text = "123"; say(text); int a = 10; say(a); say("123"); } public static void say(Object o){//多态的体现 System.out.println(o); }
2.2 Object其他方法
2.2.1 equals(Object obj)
equals(Object obj)源码:
public boolean equals(Object obj) {
return (this == obj);
}
- equals方法在非null对象引用上实现等价关系
- 等于
- 对于任何非空引用值x和y,当且仅当x和y引用同一对象( x == y具有值true )时,此方法返回true
- ==比的是内存地址
- 请注意,通常需要在重写此方法时覆盖hashCode方法,以便维护hashCode方法的常规协定,该方法声明相等对象必须具有相等的哈希代码。
public static void main(String[] args) { Person p = new Person(1234,"张三"); Person e = new Person(2345,"李四"); System.out.println("p:"+p.hashCode());//p:1239731077 System.out.println("e:"+e.hashCode());//e:357863579 System.out.println(p.equals(e));//false e=p;//此时指向相同的内存地址 System.out.println("e:"+e.hashCode());//e:1239731077 System.out.println(p.equals(e));//true }
输出结果:
p:1239731077
e:357863579
false
e:1239731077
true
equals方法重写时的五个特性:
自反性 :对于任何非空的参考值x , x.equals(x)应该返回true 。
对称性 :对于任何非空引用值x和y , x.equals(y)应该返回true当且仅当y.equals(x)返回true 。
传递性 :对于任何非空引用值x ,y和z ,如果x.equals(y)回报true并且y.equals(z)返回true,x.equals(z)应该返回true 。
一致性 :对于任何非空引用值x和y ,多次调用x.equals(y)始终返回true或始终返回false ,前提是未修改对象上的equals比较中使用的信息。
非空性 :对于任何非空的参考值x , x.equals(null)应该返回false 。
2.2.2 toString()
toString()源码
public String toString() {
return getClass().getName() + “@” + Integer.toHexString(hashCode());
}
默认为字符串,通常返回一个“文本表示”此对象的字符串,返回对象的内存地址(对象实例的类名称@对象的哈希码的无符号十六进制,即:getClass().getName() + ‘@' + Integer.toHexString(hashCode()))
public static void main(String[] args) { Person p = new Person(1234,"张三"); Person e = new Person(2345,"李四"); System.out.println("e.getClass():"+e.getClass()); System.out.println("e.hashCode():"+e.hashCode()); System.out.println("e.toString():"+e.toString()); }
输出结果:
e.getClass():class com.company.demo.Person
e.hashCode():114132791
e.toString():com.company.demo.Person@6cd8737
- 建议重写Object中的toString方法
2.3 Object源码
package java.lang; import jdk.internal.HotSpotIntrinsicCandidate; public class Object { private static native void registerNatives(); static { registerNatives(); } @HotSpotIntrinsicCandidate public Object() {} @HotSpotIntrinsicCandidate public final native Class<?> getClass(); @HotSpotIntrinsicCandidate public native int hashCode(); public boolean equals(Object obj) { return (this == obj); } @HotSpotIntrinsicCandidate protected native Object clone() throws CloneNotSupportedException; public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } @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); } protected void finalize() throws Throwable { } }
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注自由互联的更多内容!