Java object details

Source: Internet
Author: User

Post from: http://hi.baidu.com/java4me/blog/item/76ca8407338ed2720208818d.html

 

Constructor Summary
Object ()

Method Summary
Protected object clone ()
Create and return a copy of this object.
Boolean equals (Object OBJ)
Indicates whether another object is "equal" to this object ".
Protected void finalize ()
When the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method.
Class <? Extends Object> getclass ()
Returns the runtime class of an object.
Int hashcode ()
Returns the hash value of the object.
Void notify ()
Wake up a single thread waiting on this object monitor.
Void policyall ()
Wake up all threads waiting on this object monitor.
String tostring ()
Returns the string representation of the object.
Void wait ()
This causes the current thread to wait until other threads call the notify () method or the notifyall () method of this object.
Void wait (long timeout)
This causes the current thread to wait until other threads call the notify () method or the yyall () method of this object, or exceed the specified time.
Void wait (long timeout, int Nanos)
This causes the current thread to wait until other threads call the notify () method or yyall () method of this object, or other threads interrupt the current thread, or the thread has exceeded the actual time.

Constructor details

Object
Public object ()
Method details

Getclass
Public final class <? Extends Object> getclass ()
Returns the runtime class of an object. This class object is locked by the static Synchronized Method of the class.

Return Value:
Indicates the java. Lang. Class Object of the runtime class of this object. This result belongs to the type class <? Extends x>, where X indicates to clear the static type in the expression. This expression calls getclass.

--------------------------------------------------------------------------------

Hashcode
Public int hashcode ()
Returns the hash value of the object. This method provides some advantages for the hash table, such as the hash table provided by Java. util. hashtable.
The general protocol of hashcode is:

During Java application execution, when calling the hashcode method 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 is not modified. This 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, the hashcode method must be called on each object of the two objects to generate the same integer result.
The following situations are not required. lang. object) method. If the two objects are not equal, calling the hashcode Method on any of the two objects will certainly generate different integer results. 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 generally achieved by converting the internal address of the object into an integer, but this implementation technique is not required by the javatm programming language .)

Return Value:
A hash value of this object.
For more information, see:
Equals (Java. Lang. Object), hashtable

--------------------------------------------------------------------------------

Equals
Public Boolean equals (Object OBJ)
Indicates whether another object is "equal" to this object ".
The equals method achieves equality on non-null object references:

Self-inverse: returns true for any non-null reference value X, X. Equals (X.
Symmetry: For any non-null reference values X and Y, X. Equals (y) returns true only when Y. Equals (x) returns true.
Passed: For any non-null reference values X, Y, and Z, if X. equals (y) returns true, and Y. equals (z) returns true, then X. equals (z) should return true.
Consistency: For any non-null reference values X and Y, call X multiple times. equals (y) always returns true or false, provided that the information used in the equals comparison on the object is not modified.
Returns false for any non-null reference value X, X. Equals (null.
The equals method of the object class implements the maximum possible equality relationship between objects. That is, for any non-null reference values X and Y, if and only when x and y reference the same object, this method returns true (x = Y has a value of true ).

Note: When this method is overwritten, it is usually necessary to override the hashcode method to maintain the conventional protocol of the hashcode method, which declares that equal objects must have equal hash codes.

Parameters:
OBJ-the referenced object to be compared.
Return Value:
If the object is the same as the OBJ parameter, true is returned; otherwise, false is returned.
For more information, see:
Hashcode (), hashtable

--------------------------------------------------------------------------------

Clone
Protected object clone ()
Throws clonenotsupportedexception
Create and return 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 the expression:
X. Clone ()! = X
If yes, the expression is:
X. Clone (). getclass () = x. getclass ()
True, but these are not absolute conditions. Generally:
X. Clone (). Equals (X)
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 its superclasses (except objects) comply with this convention, X. Clone (). getclass () = x. getclass ().

By convention, the object returned by this method should be independent of this object (the object being cloned ). To obtain this independence, it is necessary to modify one or more fields of this object before Super. clone returns an object. This usually means copying all the variable objects that contain the internal "deep structure" of the cloned object, and replacing the references to these objects with references to the copy. If a class only contains basic fields or references to unchanged objects, you do not need to modify the fields in the objects returned by Super. Clone.

Object Class clone method to perform specific clone operations. First, if the class of this object cannot implement the interface cloneable, clonenotsupportedexception will be thrown. Note: All arrays are regarded as cloneable interfaces. Otherwise, this method will create a new instance of the class of this object, and initialize all fields of this object using the content of the corresponding fields of this object just like by allocation; the content of these fields is not self-cloned. Therefore, this method executes the "superficial copy" of the object, instead of "deep copy.

The object class itself does not implement the interface cloneable. Therefore, calling the clone method on an object whose class is an object will throw an exception during runtime.

Return Value:
A clone of this instance.
Throw:
Clonenotsupportedexception-if the object class does not support the cloneable interface, the subclass of the rewrite clone method will also throw this exception to indicate that an instance cannot be cloned.
For more information, see:
Cloneable

--------------------------------------------------------------------------------

Tostring
Public String tostring ()
Returns the string representation of the object. Generally, the tostring method returns a string that "represents the object in text. The results should be concise but easy to understand. We recommend that you override this method for all subclasses.
The tostring method of the object class returns a string consisting of the Class Name (the object is an instance of the class) the at tag "@" and the unsigned hexadecimal representation of the object's hash code. In other words, this method returns a string whose value is equal:

Getclass (). getname () + '@' + integer. tohexstring (hashcode ())

Return Value:
String Representation of the object.

--------------------------------------------------------------------------------

Notify
Public final void Policy ()
Wake up a single thread waiting on this object monitor. If all threads are waiting on this object, the system will wake up one of the threads. The choice is arbitrary and occurs when a decision is made on the implementation. The thread waits on the object's monitor by calling one of the wait methods.
The wake-up thread can continue to be executed until the current thread abandons the lock on this object. The wake-up thread will compete with all other threads that actively Synchronize on the object in the conventional way; for example, the wake-up thread has no reliable privilege or disadvantage in serving as the next thread to lock this object.

This method should be called only by the thread that is the owner of this object monitor. One of the following three methods allows a thread to become the owner of the object monitor:

Run the sychronized instance method of this object.
The body of the synchronized statement that is synchronized on this object.
For objects of the class type, you can execute the synchronized static method of this class.
Only one thread can own the monitor of the object at a time.

Throw:
Illegalmonitorstateexception-if the current thread is not the owner of this object monitor.
For more information, see:
Policyall (), wait ()

--------------------------------------------------------------------------------

Policyall
Public final void policyall ()
Wake up all threads waiting on this object monitor. The thread waits on the object's monitor by calling one of the wait methods.
The wake-up thread can continue to be executed until the current thread abandons the lock on this object. The wake-up thread will compete with all other threads that actively Synchronize on the object in the conventional way; for example, the wake-up thread has no reliable privilege or disadvantage in serving as the next thread to lock this object.

This method should be called only by the thread that is the owner of this object monitor. Refer to the notify method to learn about the method that a thread can become the monitor owner.

Throw:
Illegalmonitorstateexception-if the current thread is not the owner of this object monitor.
For more information, see:
Notify (), wait ()

--------------------------------------------------------------------------------

Wait
Public final void wait (long timeout)
Throws interruptedexception
This causes the current thread to wait until other threads call the notify () method or the yyall () method of this object, or exceed the specified time.
The current thread must own this object monitor.

This method causes the current thread (T) to place itself in the waiting set of the object, and then discard all the synchronization requirements on the object. For the purpose of thread scheduling, thread t is disabled and in sleep state until one of the following four situations occurs:

Another thread calls the notify method of this object, and thread t is chosen as the wake-up thread.
Another thread calls the yyall method of this object.
Other threads interrupt thread t.
The specified actual time has been reached. However, if the timeout value is zero, the actual time is not taken into account, and the thread will wait until it is notified.
Then, thread t is deleted from the object's waiting set and thread scheduling is re-executed. Then, the thread competes with other threads in a conventional manner to obtain the right to synchronize on the object. Once the thread obtains control over the object, all the synchronous declarations on the object will be restored to the previous state-this is the case when the wait method is called. Then, the thread t is returned from the wait method call. Therefore, the synchronization status of the object and thread t is the same as that of the wait method.
The thread can also wake up a so-called spurous wakeup without being notified, interrupted, or timed out ). Although this rarely happens in practice, the application must use the following methods to prevent it from happening, that is, to test the conditions that should cause the thread to be reminded. If this condition is not met, then wait. In other words, the wait should always occur in the loop, as shown in the following example:

Synchronized (OBJ ){
While (<condition does not hold>)
OBJ. Wait (timeout );
... // Perform action appropriate to Condition
}
(For more information about this topic, see concurrent programming in Java (second edition) by Doug LEA (Addison-Wesley, 2000) section 3.2.3 or section 2001 of the valid Java programming language guide (Addison-Wesley, 50th) written by Joshua Bloch.
If the current thread is interrupted by another thread while waiting, interruptedexception will be thrown. This exception is thrown only when the lock status of this object is restored in the preceding format.

Note: Because the wait method puts the current thread into the object's waiting set, it can only unlock this object; other objects in the current thread can be synchronized and remain locked when the thread waits.

This method should be called only by the thread that is the owner of this object monitor. Refer to the notify method to learn about the method that a thread can become the monitor owner.

Parameters:
Timeout-maximum wait time (in milliseconds ).
Throw:
Illegalargumentexception-if the timeout value is negative.
Illegalmonitorstateexception-if the current thread is not the owner of this object monitor.
Interruptedexception-if the other thread interrupts the current thread before the current thread waits for the notification or is waiting for the notification. When this exception is thrown, the interruption status of the current thread is cleared.
For more information, see:
Policy (), policyall ()

--------------------------------------------------------------------------------

Wait
Public final void wait (long timeout,
Int Nanos)
Throws interruptedexception
This causes the current thread to wait until other threads call the notify () method or yyall () method of this object, or other threads interrupt the current thread, or the thread has exceeded the actual time.
This method is similar to the wait method of a parameter, but it allows better control of the amount of time waiting for notification before giving up. The actual time measured in milliseconds can be calculated using the following formula:

1000000 * timeout + Nanos
In all other respects, the operation performed by this method is the same as that of the wait (long) method with a parameter. In particular, wait (0, 0) is the same as wait (0.

The current thread must own this object monitor. This thread releases ownership of this monitor and waits for one of the following two conditions to occur:

Other threads notify the waiting thread on the monitor of this object to wake up by calling the notify method or the notifyall method.
The time-out period specified for the sum of the timeout millisecond value and the nanos millisecond parameter value is used up.
Then, the thread will not continue execution until it obtains ownership of the monitor again.

For a parameter version, it is possible to implement interruption and false wakeup, and this method should always be used in the Loop:

Synchronized (OBJ ){
While (<condition does not hold>)
OBJ. Wait (timeout, Nanos );
... // Perform action appropriate to Condition
}
This method should be called only by the thread that is the owner of this object monitor. Refer to the notify method to learn about the method that a thread can become the monitor owner.

Parameters:
Timeout-maximum wait time (in milliseconds ).
Nanos-additional time (in microseconds, range: 0-999999 ).
Throw:
Illegalargumentexception-if the timeout value is a negative number, or the millisecond value is not in the range of 0-999999.
Illegalmonitorstateexception-if the current thread is not the owner of this object monitor.
Interruptedexception-if the current thread is waiting for a notification or is waiting for a notification, other threads interrupt the current thread. When this exception is thrown, the interruption status of the current thread is cleared.

--------------------------------------------------------------------------------

Wait
Public final void wait ()
Throws interruptedexception
This causes the current thread to wait until other threads call the notify () method or the notifyall () method of this object. In other words, the behavior of this method is as if it only executes the wait (0) Call.
The current thread must own this object monitor. This thread releases ownership of this monitor and waits until other threads wake up on the monitor of this object by calling the notify method or the notifyall method. Then the thread will not continue execution until it obtains ownership of the monitor again.

For a parameter version, interruption and false wakeup are possible, and this method should always be used in a loop:

Synchronized (OBJ ){
While (<condition does not hold>)
OBJ. Wait ();
... // Perform action appropriate to Condition
}
This method should be called only by the thread that is the owner of this object monitor. Refer to the notify method to learn about the method that a thread can become the monitor owner.

Throw:
Illegalmonitorstateexception-if the current thread is not the owner of this object monitor.
Interruptedexception-if the other thread interrupts the current thread before the current thread waits for the notification or is waiting for the notification. When this exception is thrown, the interruption status of the current thread is cleared.
For more information, see:
Policy (), policyall ()

--------------------------------------------------------------------------------

Finalize
Protected void finalize ()
Throws throwable
When the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method. Subclass override the Finalize method to configure system resources or execute other cleanup operations.
The general protocol of finalize is that this method is called when the javatm virtual machine has determined that any thread that has not been terminated cannot access this object in any way, unless an operation is performed for the end operation of another object or class to be terminated. The Finalize method can take any action, including re-enabling this object to be available to other threads; however, the main purpose of finalize is to clear the object before it can be unundone. For example, the Finalize method of the input/output connection object can execute an explicit I/O transaction to interrupt the connection before the object is permanently discarded.

The Finalize method of the object class performs non-special operations; it only performs some common responses. This definition can be rewritten as a subclass of an object.

The Java programming language does not guarantee which thread will call the Finalize method of a given object. However, when you call finalize, the thread that calls finalize will not hold any visible synchronization locks. If the Finalize method throws an uncaptured exception, the exception is ignored and the end operation of the object is terminated.

After the Finalize method of an object is enabled, no further operations will be performed until the Java Virtual Machine confirms again that no threads have been terminated and cannot access this object in any way, these include the possible operations performed by other objects or classes to be terminated. When this operation is performed, the objects may be discarded.

For any given object, the Java Virtual Machine can only call the Finalize method once.

Any exception thrown by the Finalize method will stop the end operation of this object, but you can ignore it by other methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.