当前位置:网站首页>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;
}
}
边栏推荐
- It is forbidden to splice SQL in code
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- Solve the problem that the kaggle account registration does not display the verification code
- Subgraph isomorphism -subgraph isomorphism
- ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
- Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
- D26: the nearest number (translation + solution)
- How to make icons easily
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
猜你喜欢
Similarities and differences of text similarity between Jaccard and cosine
Iclr2022: how does AI recognize "things I haven't seen"?
[PHP basics] session basic knowledge, application case code and attack and defense
[CSDN Q & A] experience and suggestions
Tencent interview: can you find the number of 1 in binary?
2/14 (regular expression, sed streaming editor)
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
[source code] VB6 chat robot
[PHP basics] cookie basics, application case code and attack and defense
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
随机推荐
Selenium library 4.5.0 keyword explanation (III)
Stock price forecast
How to trade spot gold safely?
What are the application fields of digital twins in industry?
D30:color tunnels (color tunnels, translation)
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
Social network analysis -social network analysis
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
Fudan 961 review
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Selenium check box
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
D24:divisor and multiple (divisor and multiple, translation + solution)
BBS forum recommendation
MySQL is installed as a Windows Service
Idea a method for starting multiple instances of a service
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Data storage - interview questions
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
AI Challenger 2018 text mining competition related solutions and code summary