411 REFERENCE · Wikibooks
Java Programming/API/java.lang.Object
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
java.lang.Object
class is the superclass of all Java classes. All Java classes inherited from this class. This makes it possible that we can have methods that are available in all Java classes. This simplifies things compared to C++ where this is not the case.
class methods Description equals( o ); Gives generic way to compare objects getClass(); The class gives us more information about the object hashCode(); Returns a hash value that is used to search objects in a collection notify(); Used in synchronizing threads notifyAll(); Used in synchronizing threads toString(); Can be used to convert the object to String wait(); Used in synchronizing threads clone() throws CloneNotSupportedException ; Return a new object that are exactly the same as the current object finalize() throws Throwable; This method is called just before an object is garbage collected
equals() Method
The equals( o ) method gives a generic way to compare objects for equality. You need to override it, in your class. Then you can write:
isCustomerExist( Customer newCustomer )
{
isRet = ;
Iterator iter = _collAllCustomer.iterator();
( iter.hasNext() )
{
( newCustomer.equals( (Customer) iter.next() )
{
// -- Customer was found ---
isRet = ;
}
}
return isRet;
}
Keep in mind that when you override equals(), you always need to also override hashCode() so the two methods are consistent. If two objects are equal, they must have the same hashcode.
For more information also see Java Programming/Comparing Objects
getClass() Method
There is a object for each class in your program. Every array also belongs to a class that is reflected as a object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (, , , , , , , and ), and the keyword are also represented as objects. Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded
For more information see .
The most popular use of the is to find out the object's class name during runtime.
com.yourCompany.Customer;
...
Object obj = new Customer();
...
System.out.println( "Name:" + obj.getClass().getName() );
The output:
Name: com.yourCompany.Customer
hashCode() Method
In most cases you should not override this method, since the default implementation of this method returns a unique number for the object. The number is used when the object is put into a collection. Finding an object in a big collection may take a while, if objects are compared one by one sequentially. To speed the search up, objects may be placed in a tree structure, weighted by an integer hash code. Comparing the hash code while navigating through the tree, the number of object comparisons can be reduced.
_______ A _____
| |
__ B__ __C__
| | | |
D E F G
... ... ... ...
To give you a general idea of how it may work, see the above diagram. Let's say we are searching object G. If at each 'node' of the tree we can decide which way to go, then by 3 steps we reach the object G.
By constrast in a linear search:
A --- B ----- C ---- C ---- D ---- E ---- F ---- G
We would need 8 steps to reach the object G.
So the search will be faster with the tree structure. Adding a new object however, will be slower because the tree structure needs to be maintained. The place of the new object in the tree has to be found first.
toString() Methods
This method can be used to convert an object to a . It is automatically used in many places to convert objects to String; for example: in PrintStream, in StringBuffer, and for the string concatenation operator when used on objects.
The default implementation returns a weird string with the class name and the hash code.
For example:
str = "This customer is " + objCust;
The toString() method is called on the objCust object.
The toString() method can also be used for debugging:
Customer
{
_name;
_address;
_age;
...
toString()
{
StringBuffer buf = new StringBuffer();
buf.append( "Name = " ); buf.append( _name ); buf.append( "\n" );
buf.append( "Address= " ); buf.append( _address ); buf.append( "\n" );
buf.append( "Age = " ); buf.append( _age ); buf.append( "\n" );
...
return buf.toString();
}
...
}
After that whenever in your code, you want to see what a customer object is, just call:
System.out.println( objCustomer );
Synchronizing Threads Methods
See also:
Java Programming/API/java.lang.Class
Java Programming/API/java.lang.Thread
deep