Mentioned Java
, it would have to say Object
class. In the interview, you may also often encounter Object
similar questions, which are summarized here for your reference only.
Object
Class is Java
the root class of all classes in , that is, all classes inherit from Object
class. It includes the following methods:
protected Object clone() throws CloneNotSupportedException
A copy class that creates and returns an object. In general for any object x,
-returned x.clone() != x
as true
-returned x.clone().getClass() == x.getClass()
as true
-returned x.clone().equals(x)
astrue
If the object needs to be used clone
, it needs to implement the Cloneable
interface. Cloneable
An interface is just a marker, it doesn't have any methods.
For a more in-depth understanding, please refer to https://www.cnblogs.com/Qian123/p/5710533.html for object cloning and replication (including deep copy, shallow copy, etc.)
public boolean equals(Object obj)
Determines whether two objects are the same object. equals
Methods cannot act on null
objects and have the following characteristics:
- Reflexive
For any non-null reference value x
, always exists x.equals(x)
as true
-symmetry
For any non-null reference value x
, , must also return if it y
exists - transitivityx.equals(y)
true
y.equals(x)
true
For any non-null reference value x
, y
, z
, exists x.equals(y)
as true
, then y.equals(z)
also as true
, x.equals(z)
must also return true
- Consistency
For any non-null reference value x
, always returns y
, regardless of how many times it is called , or - For any non-null reference value , always exists asx.equals(y)
true
false
x
x.equals(null)
false
When overriding equals()
a method, you should also override the hashCode()
method
In the interview, I often ask "please talk about
Object
the difference between the method==
andequals()
the method":
in fact,Object
there is no difference between the two in the class, and both indicate whether the memory addresses pointed to by the two objects are the same. However, dueJava
to the polymorphism feature, subclasses can overrideequals()
methods. For example, theString
classequals()
represents whether the value of two strings is the same.
protected void finalize()
This method is called when the GC (garbage collector) determines that the object does not have any references. Subclasses can override this method to free system resources or perform other cleanup.
The JVM virtual machine cannot determine which thread to call the finalize()
method of the object, but it can be sure that finalize()
the thread calling the method will not block the user thread. If an exception is thrown while executing the method, the method of the corresponding object finalize()
terminates execution. Also, each object will only execute the finalize()
method once.
public final Class<?> getClass()
Returns the class of this object at runtime. The returned class object is static
the object locked by the lock method.
int hashCode()
Return the hash value for the object. The conventions are as follows:
- If the same object is called multiple times during the same execution of the program, the hash value must return the same. But not the same calling process is not necessarily the same;
- If the two objects call equals()
the same method, the hashCode()
method return value must be the same;
- If the two objects call equals()
the method is not the same, the hash value of the two objects does not need to be different. But programming must be aware that different objects produce different hash values can improve HashTable
the performance of a class like this.
void notify()
Wake up a single thread waiting for an object lock. If there are multiple threads waiting, one of them will be woken up randomly, competing for locks with other threads that want to occupy the object lock.
void notifyAll()
Wake up all threads waiting for an object lock. The awakened thread competes for the lock with other threads that want to hold the object lock.
String toString()
Returns the String
representation of the object, default is getClass().getName() + '@' + Integer.toHexString(hashCode())
. JDK specification requirements: Generally, in order to clearly print out object properties, it is recommended to override this method.
In the usual project, the author will write a
Base
class, override thetoString
method, and return aJSON
string. Make all custom classes inherit fromBase
this class .
void wait()
Called by the current thread that occupies the object lock, and releases the owned object lock until another thread calls notify()
or the notifyAll()
method wakes up.
When a thread calls a wait()
method, there may be false wakeups, so this method must be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
// Perform action appropriate to condition
}
void wait(long timeout)
It is called by the current thread that occupies the object lock, and the owned object lock is released until other threads call notify()
or notifyAll()
method wakes up or automatically wakes up after a specified time.
void wait(long timeout, int nanos)
It is called by the current thread that occupies the object lock, and the owned object lock is released until other threads call notify()
or notifyAll()
methods wake up or automatically wake up after the specified time (1000000*timeout + nanos).