Thursday, June 4, 2009

How to compair two objects in java

 
// exact match for a class.
if (object.getClass() == MyClass.class)
{
    ...
}
 
// Determines if the specified Object is assignment-compatible 
// with the object represented by this Class. This method is the 
// dynamic equivalent of the Java language instanceof operator. 
// The method returns true if the specified Object argument is 
// non-null and can be cast to the reference type represented by 
// this Class object without raising a ClassCastException. 
// It returns false otherwise.
if (MyClass.isInstance(obj))
{
...
}