Class object is the root class of a class hierarchy, and each class uses object as a superclass, and all objects, including arrays, implement methods of this class. The 11 methods in this class are described in jdk1.5.
1.getClass
-
Returns the run-time class of an object. The class object is an object that is locked by the static synchronized method of the represented class.
-
-
Return:
An
-
object that represents the run-time class of the object
java.lang.Class
. This result belongs to
Class<? extends X>
a type, where X represents a static type in the purge expression that is called by the expression
getClass
.
2.hashCode
-
Returns the hash code value of the object. This method is supported to provide some advantages to the hash table, for example, a
java.util.Hashtable
hashtable provided.
hashCode
The General agreement is:
- During Java application execution, when the hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the equals comparison on the object has not been modified. The integer does not need to be consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the equals (object) method, calling a method on each object in two
hashCode
must produce the same integer result.
- The following conditions are not required: If
equals(java.lang.Object)
two objects are not equal according to the method, then calling the hashcode method on any of the two objects must produce a different integer result. However, programmers should know that generating different integer results for unequal objects can improve the performance of the hash table.
In fact, the Hashcode method defined by the object class does return different integers for different objects. (This is typically done by translating the object's internal address into an integer, but the JavaTM programming language does not require this implementation technique.) )
-
-
Return:
-
a hash code value for this object.
3.equals
-
Indicates whether a different object is "equal" to this object.
equals
method to implement an equality relationship on a non-null object reference:
- reflexive : For any non-null reference value
x
, x.equals (x)
should return true
.
- symmetry : For any non-null reference value
x
and y
, when and only if y.equals (x)
returns &NBSP; true
, x.equals (y)
should return true
.
- transitivity : for any non-null reference value,
x
, y
and z
, if X.equals (y)
return true
, and y.equals (z)
return true
, then x.equals (z)
should return true
.
- Consistency : for any non-null reference value,
x
and y
, multiple calls to x.equals (y) always returns &NBSP; true
or always returns &NBSP; false
, if the object is equals
The information used in the comparison has not been modified.
- for any non-null reference value
x
, x.equals (null)
should return false
.
Object
The equals method of a class implements the most differentiated equality relationship on an object, that is, for any non-null reference value x
and y
, if and only if the x
y
same object is referenced, this method returns true
( x == y
with a value true
).
Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares that the equality object must have an equal hash code.
-
-
-
Parameters:
-
obj
-the Reference object to compare with.
-
Return:
-
returns if this object is the same as the obj parameter
true
;
false
4.clone
Clone () throws Clonenotsupportedexception
-
Creates and returns a copy of this object. The exact meaning of a "copy" may depend on the class of the object. In general, for any object x, if an expression:
is correct, the expression is:
X.clone (). GetClass () = = X.getclass ()
will be for true, but these are not absolute conditions. In general the following are:
will be for true, but this is not an absolute condition.
By convention, the returned object should be obtained by calling Super.clone . If a class and all of its superclass (exceptObject ) adhere to this Convention, then X.clone (). GetClass () = = X.getclass ().
By convention, the object returned by this method should be independent of the object (the object being cloned). To achieve this independence, it is necessary to modify one or more fields of the object before super.clone returns the object. This usually means that you want to copy all the mutable objects that contain the internal "deep structure" of the object being cloned and replace the references to those objects with references to the copy. If a class contains only basic fields or references to immutable objects, you typically do not need to modify the fields in the object returned by Super.clone .
The clone method of the Object class performs a specific clone operation. First, if the class of this object cannot implement interface cloneable, then clonenotsupportedexceptionwill be thrown. Note: All arrays are considered to implement Interface cloneable. Otherwise, this method creates a new instance of the class for this object and, as it is assigned, initializes all fields of the object strictly using the contents of the corresponding field of the object, and the contents of those fields are not self-cloned. Therefore, this method performs a "shallow copy" of the object, not a "deep copy" operation.
The object class itself does not implement interface cloneable, so calling the clone method on an object that is class object will cause an exception to be thrown at run time.
-
-
-
Return:
-
A clone of this instance.
-
Thrown:
-
CloneNotSupportedException
-If the object's class does not support
Cloneable
an interface, the
clone
subclass of the overridden method also throws this exception to indicate that an instance cannot be cloned.
5.toString
-
Returns the string representation of the object. Typically, the
toString
method returns a string that represents this object as text. The result should be a concise but easy-to-read understanding. It is recommended that all subclasses override this method.
Object
The method of the class toString
returns a string consisting of the class name (the object is an instance of the class), the at marker " @
", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:
GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())
-
-
Return:
-
the string representation of the object.
6.notify
-
Wakes up a single thread waiting on this object monitor. If all threads are waiting on this object, then one of the threads is chosen to wake up. The choice is arbitrary and occurs when a decision is made on the implementation. The thread
wait
waits on the object's monitor by calling one of the methods.
Until the current thread discards the lock on this object, it can continue to execute the awakened thread. The awakened thread competes with all other threads that are actively synchronizing on the object, for example, the thread that wakes up does not have a reliable privilege or disadvantage as the next thread that locks the object.
This method should be called only by threads that are the owner of this object monitor. A thread can be the owner of this object monitor by one of the following three methods:
- By executing this object's synchronous (sychronized) instance method.
- The body of the statement that is synchronized by executing on this object
synchronized
.
- For
Class
types of objects, you can do this by executing a synchronous static method of the class.
Only one thread can have a monitor for an object at a time.
-
-
Thrown:
-
IllegalMonitorStateException
-If the current thread is not the owner of this object monitor.
7.notifyAll
-
Wakes all the threads waiting on this object monitor. The thread
wait
waits on the object's monitor by calling one of the methods.
Until the current thread discards the lock on this object, it can continue to execute the awakened thread. The awakened thread competes with all other threads that are actively synchronizing on the object, for example, the thread that wakes up does not have a reliable privilege or disadvantage as the next thread that locks the object.
This method should be called only by threads that are the owner of this object monitor. See notify
methods for a description of the methods that a thread can become a monitor owner.
-
-
Thrown:
-
IllegalMonitorStateException
-If the current thread is not the owner of this object monitor.
8.finalize
Finalize () throws Throwable
-
-
This method is called by the object's garbage collector when the garbage collector determines that no more references to the object exist. Subclass overrides
Finalize
methods to configure system resources or perform other cleanup. The general contract for
Finalize is: This method is called when the Java TM Virtual machine has determined that any thread that has not been terminated can no longer access this object by any means. Unless an action is performed by an end operation of an object or class that is ready to terminate. The Finalize method can take any action, including making the object available again to other threads, but the primary purpose of finalize is to perform a purge operation before the object is irrevocably discarded. For example, the Finalize method of an object representing an input/output connection can perform an explicit I/O transaction so that the connection is interrupted before the object is permanently discarded.
Object Finalize method performs a non-specific operation; It performs only a few general returns. Subclasses of Object can override this definition. The
Java programming language does not guarantee which thread will invoke the of a given object; Finalize method. However, it is guaranteed that when finalize is called, the thread that called Finalize will not hold any user-visible synchronization locks. If the Finalize method throws an uncaught exception, the exception is ignored, and the object's finalization operation terminates.
After enabling an object's Finalize method, no further action will be taken until the Java virtual machine determines again that any thread that has not been terminated can no longer access the object by any means. This includes possible actions performed by other objects or classes that are ready to terminate, and the object may be discarded when the operation is performed.
For any given object, the Java virtual machine calls at most one Finalize method. Any exception thrown by the
Finalize
method will cause the end operation of this object to stop, but it can be ignored by other methods.
-
-
-
Thrown:
-
Throwable
-This method throws the
Exception
9.wait
wait (Long timeout) throws Interruptedexception
-
Causes the current thread to wait until another thread calls this object's
notify()
Method or
notifyAll()
method, or more than the specified amount of time.
The current thread must have this object monitor.
This method causes the current thread (called T ) to place itself in the object's wait set, and then discards all synchronization requirements on the object. For thread scheduling purposes, the thread T is disabled and dormant until one of the following four conditions occurs:
- One of the other threads calls the notify method of this object, and the thread T happens to be selected as the thread that is awakened.
- One of the other threads calls the Notifyall method of this object.
- One of the other thread
中断
threads T .
- The actual time specified has been reached. However, if timeout is zero, the actual time is not considered, and the thread waits until notification is received.
Then, remove the thread from the object's waiting set Tand re-thread scheduling. The thread then competes with other threads in a normal way to gain the right to synchronize on that object, and once control of the object is obtained, all its synchronous declarations on that object are restored to the previous state-this is called waitmethod is the case. Then, the thread TFrom waitMethod is returned in the call to the So, from waitWhen the method returns, the object and the thread TThe synchronization state and the call waitMethod is exactly the same.
The thread can also wake up with a so-called spurious wakeup (spurious wakeup) without being notified, interrupted, or timed out. Although this situation rarely occurs in practice, the application must prevent it from occurring by testing the condition that should cause the thread to be alerted, and then continue waiting if the condition is not met. In other words, the wait should always occur in the loop, as in the following example:
Synchronized (obj) {while (<condition does not hold>) obj.wait (timeout), ...//Perform action appropriate to Conditio N
(For more information on this topic, see 3rd. 2.3 or Addison-wesley B in "Concurrent Programming in Java (Second Edition)" (Joshua, 2000) written by Doug Lea The 50th item in the effective Java Programming Language Guide (Addison-wesley, 2001), written by Loch.
If the current thread waits for another thread 中断
, it throws a interruptedexception. This exception is thrown when the lock state of this object is resumed as described above.
Note that because the wait method places the current thread in the object's waiting set, it can only unlock the object, and any other objects that can synchronize the current thread are still locked while the threads are waiting.
This method should be called only by threads that are the owner of this object monitor. See notify
methods for a description of the methods that a thread can become a monitor owner.
-
Parameters:
-
timeout
-The maximum time, in milliseconds, to wait.
-
Thrown:
-
IllegalArgumentException
-If the timeout value is negative.
-
IllegalMonitorStateException
-If the current thread is not the owner of this object monitor.
-
InterruptedException
-If another thread interrupts the current thread before the current thread waits for notification or while it is waiting for notification. When this exception is thrown, the
interrupt state of the current thread is cleared.
10.wait
wait (Long Timeout,int Nanos) throws Interruptedexception
-
Causes the current thread to wait until another thread calls this object's
notify()
Method or
notifyAll()
method, or one of the other threads interrupts the current thread, or has exceeded an actual amount of time.
This method is similar to a method of a parameter wait
, but it allows for better control over the amount of time to wait for notification before discarding. The actual amount of time measured in nanoseconds can be calculated using the following formula:
In all other respects, this method performs the same action as a method with one parameter wait(long)
. It should be noted thatwait (0, 0) is the same as wait (0) .
The current thread must have this object monitor. The thread publishes ownership of this monitor and waits for one of the following two conditions to occur:
- Other threads wake up by calling a
notify
method, or by notifyAll
means of a method that notifies the waiting thread on this object's monitor.
timeout
The milliseconds value nanos
and the sum of the nanosecond parameter values specify a time-out period that is exhausted.
The thread then waits to regain ownership of the monitor before it can continue execution.
For a version of a parameter, it is possible to implement interrupts and spurious wakes, and this method should always be used in loops:
Synchronized (obj) {while (<condition does not hold>) obj.wait (timeout, Nanos), ...//Perform action appropriate to C Ondition}
This method should be called only by threads that are the owner of this object monitor. Please see
notify
method to understand the description of the method that the thread can become the monitor owner.
-
Parameters:
-
timeout
-The maximum time, in milliseconds, to wait.
-
nanos
-extra time (in nanoseconds, the range is 0-999999).
-
Thrown:
-
IllegalArgumentException
-If the timeout value is negative, or the nanosecond value is not in the 0-999999 range.
-
IllegalMonitorStateException
-If the current thread is not the owner of this object monitor.
-If the current thread
-
InterruptedException
waits for a notification or is waiting for a notification, the other thread interrupts the current thread. When this exception is thrown, the
interrupt state of the current thread is cleared.
11.wait
wait () throws Interruptedexception
-
Causes the current thread to wait until another thread calls the
notify()
method or method of this object
notifyAll()
. In other words, this method behaves as if it were only executing wait (0) calls.
The current thread must have this object monitor. The thread publishes ownership of this monitor and waits until another thread wakes up by calling the notify
method, or by notifyAll
means of a method that notifies the waiting thread on this object's monitor. The thread then waits to regain ownership of the monitor before it can continue execution.
For a version of a parameter, it is possible to implement interrupts and spurious wakes, and this method should always be used in loops:
Synchronized (obj) {while (<condition does not hold>) obj.wait () ...//Perform action appropriate to condition}
This method should be called only by threads that are the owner of this object monitor. See
notify
methods for a description of the methods that a thread can become a monitor owner.
-
Thrown:
-
IllegalMonitorStateException
-If the current thread is not the owner of this object monitor.
-
InterruptedException
-If another thread interrupts the current thread before the current thread waits for notification or while it is waiting for notification. When this exception is thrown, the
interrupt state of the current thread is cleared.
A few common methods in Java