You are on page 1of 3

Java.lang.

object
Public Methods:
public boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal.
In order to be equal, o must represent the same object as this instance using a
class-specific comparison. The general contract is that this comparison should
be reflexive, symmetric, and transitive. Also, no object reference other than null
is equal to null.
The default implementation returns true only if this == o. The general contract
for the equals and hashCode () methods is that if equals returns true for any two
objects, then hashCode () must return the same value for these objects. This
means that subclasses of Object usually override either both methods or neither
of them.
Parameters
1. Zero: The object to compare this instance with.
Returns
true if the specified object is equal to this Object; false otherwise.
public final Class<?> getClass ()
Returns the unique instance of Class that represents this object's class. Note
that getClass() is a special case in that it actually returns Class where Foo is the
erasure of the type of the expression getClass() was called upon.
As an example, the following code actually compiles, although one might think
it shouldn't:
List l = new ArrayList();

2.getClass():
   Class c = l.getClass();
Returns
this object's Class instance.
3.hashCode():
public int hashCode ()
Returns an integer hash code for this object. By contract, any two objects for
which equals(Object) returns true must return the same hash code value. This
means that subclasses of Object usually override both methods or neither
method.
Note that hash values must not change over time unless information used in
equals comparisons also changes.
4.toString ()
Returns
this object's hash code.
public String toString ()
Returns a string containing a concise, human-readable description of this object.
Subclasses are encouraged to override this method and provide an
implementation that takes into account the object's type and data. The default
implementation is equivalent to the following expression:
 getClass().getName() + '@' + Integer.toHexString(hashCode())
5.Clone:
Returns
a printable representation of this object.
Protected Methods
protected Object clone ()
Creates and returns a copy of this Object. The default implementation returns a
so-called "shallow" copy: It creates a new instance of the same class and then
copies the field values (including object references) from this instance to the
new instance. A "deep" copy, in contrast, would also recursively clone nested
objects. A subclass that needs to implement this kind of cloning should
call super.clone() to create the new instance and then create deep copies of the
nested, mutable objects.

Example for clone:


Public class Employee implements Cloneable
{
Private int employeeId;
Private String employeeName;
Private Department department;
Public Employee(int id,string name,Department dept)
{
this.employeeId=id;
this.employeeName=name;
this.department=dept();
}
//override
Protected object clone()throws CloneSupport
Public Department(int id,String name)
{
this.id=id;
this.name=name;
}
Usage of clone:
Employee original=new Employee(1,’Admin’,dept);
Employee cloned=(Employee)original.clone();

Note:
A cloned object has its own address unit.

You might also like