当前位置:网站首页>The difference between objects and objects
The difference between objects and objects
2022-07-04 00:19:00 【Hello】
One 、Object class
If a class does not specify a parent class , Default is inheritance Object class .Object Common in class 11 A way : Often used equals(),toString() Are directly used or rewritten Object These methods in it ; And then there is final native The method of decoration : Reflection requires getClass(), Required for thread operation notify(),notifyAll(),wait(long timeout); also native The method of decoration :hashCode() and clone()
Source code :
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 {
}
}
Two 、Objects class
Objects Class is final Modified class , Non inheritable , Internal methods are static Method , from jdk1.7 It was introduced at the beginning Objects class .
Source code :
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;
}
}
边栏推荐
- How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
- Gossip about redis source code 83
- Joint examination of six provinces 2017
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
- Gossip about redis source code 80
- 想请教一下,十大劵商如何开户?在线开户是安全么?
- Social network analysis -social network analysis
- What are the application fields of digital twins in industry?
- CSP window
猜你喜欢
[C language] break and continue in switch statement
Zipper table in data warehouse (compressed storage)
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
Enter MySQL in docker container by command under Linux
Unity elementary case notes of angry birds Siki college 1-6
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Qtcharts notes (V) scatter diagram qscatterseries
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
随机推荐
2020.2.14
Selenium library 4.5.0 keyword explanation (4)
SPI based on firmware library
China standard gas market prospect investment and development feasibility study report 2022-2028
Data storage - interview questions
Entropy and full connection layer
A method to solve Bert long text matching
Smart fan system based on stm32f407
P3371 [template] single source shortest path (weakened version)
Advanced C language - pointer 2 - knowledge points sorting
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
2/14 (regular expression, sed streaming editor)
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
SQL data update
NLP pre training technology development
NLP Chinese corpus project: large scale Chinese natural language processing corpus
Selenium library 4.5.0 keyword explanation (I)
Distributed transaction -- middleware of TCC -- selection / comparison
D28:maximum sum (maximum sum, translation)
BBS forum recommendation