Java Reference
In-Depth Information
toString () - provides a string representation of this object. The default for a class
consists of a string constructed from the name of the class plus the character “@ plus a
hash code of the object in hex format. This method is often overridden to provide more
illuminating information. (See the next section.)
finalize () - called by the garbage collector when there are no more references to
this object. You can override this method to take care of any housecleaning operations
needed before the object disappears.
getClass () - gets the runtime class of the object, returned as a Class type (see the
We b Course Chapter 5: Supplements section for a discussion of the Class class).
hashCode () - generates a hash code value unique for this object.
The following methods involve thread synchronization that we introduce in
Chapter 8. They can only be called from within a synchronized method or code
block:
notify () - called by a thread that owns an object's lock to tell a waiting thread, as
chosen by the JVM, that the lock is now available.
notifyAll () - similar to notify() but wakes all waiting threads and then they
compete for the lock.
wait () - the thread that owns the lock on this object releases the lock and then
waits for a notify() or notifyAll() to get the lock back.
wait (long msecs) - same as wait() but if a notify fails to arrive within the
specified time, it wakes up and starts competing for the lock on this object anyway.
wait (long msecs, int nanosecs) - same as wait (long msecs) but
specified to the nanosecond.
(We note that most operating systems do not provide a clock that is accurate to a
nanosecond and some not even to a few milliseconds.)
4.6.4 Objects to strings
We discussed in Chapter 3 how to convert primitive types to and from strings.
Yo u can also convert any Java object to a string. If you just print any object, as in
System.out.println (someObjectReference);
then that object's toString() method is called automatically to produce string
output. All objects inherit the toString() method from the Object class.
This default version of toString() from Object produces a string beginning
with the class name with certain data values appended to it.
However, the toString() method typically is overridden by most classes to
provide output in a more readable format customized for that class. Most of the
classes in the Java core class libraries provide sensible toString() methods,
and classes that you write should too for convenience when printing.
Search WWH ::




Custom Search