Java Programming/API/java.lang.Object

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.

equals Method
isCustomerExist( Customer newCustomer ) {    isRet = ; Iterator iter = _collAllCustomer.iterator; ( iter.hasNext ) {       ( newCustomer.equals( (Customer) iter.next )       {          // -- Customer was found ---          isRet = ;       }    }   return isRet; }
 * The  method gives a generic way to compare objects for equality.  You need to override it, in your class. Then you can write:

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  method is called on the   object.

The  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 );

See also:

 * Java Programming/API/java.lang.Class
 * Java Programming/API/java.lang.Thread