FAQ

Java

JSP

Servlet


Advertisement



isInstance vs. isAssignableFrom? isInstance

public boolean isInstance(Object obj)

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.

Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); it returns false otherwise.

class AClass {
public AClass() {
}
}

class BClass extends AClass {
public BClass() {
}
}

public class Program {

public static void main(String[] args) {
BClass obj1 = new BClass();

System.out.println(AClass.class.isInstance(obj1));
//true, BClass is subclass of AClass

System.out.println(BClass.class.isInstance(obj1));
//true

AClass obj2 = new AClass();
System.out.println(AClass.class.isInstance(obj2));
//true

System.out.println(BClass.class.isInstance(obj2));
//false, AClass is not subclass of BClass
}

}

If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise.

public class Program { 

public static void main(String[] args) {
Integer[] array = new Integer[4];
System.out.println( int[].class.isInstance(array)); //false
System.out.println( Integer[].class.isInstance(array));//true
System.out.println( Long[].class.isInstance(array)); //false
System.out.println( Object.class.isInstance(array)); //true
}

}

If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise.

public class Program { 

public static void main(String[] args) {
ArrayList array = new ArrayList();
System.out.println( List.class.isInstance(array)); // true
}

}

If this Class object represents a primitive type, this method returns false.

public class Program { 

public static void main(String[] args) {
int i =1;
Object obj = new Object();
System.out.println( Integer.TYPE.isInstance(i)); //false
System.out.println( Integer.TYPE.isInstance(obj)); //false
}
}

isAssignableFrom

public boolean isAssignableFrom(Class cls)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter (Class cls). It returns true if so; otherwise it returns false. For example, the Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

class AClass {
public AClass() {
}
}

class BClass extends AClass {
public BClass() {
}
}

public class Program {

public static void main(String[] args) {
System.out.println(AClass.class.isAssignableFrom(Object.class));
//false

System.out.println(Object.class.isAssignableFrom(AClass.class));
//true

System.out.println(AClass.class.isAssignableFrom(BClass.class));
//true

System.out.println(BClass.class.isAssignableFrom(AClass.class));
//false
}

}

If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

public class Program { 

public static void main(String[] args) {
System.out.println(Integer.TYPE.isAssignableFrom(Number.class));
//false

System.out.println(Integer.TYPE.isAssignableFrom(Integer.class));
 //false

System.out.println(Integer.TYPE.isAssignableFrom(Integer.TYPE));
//true
}

}

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion.

The isAssignableFrom method requires the parameter Class object which representing the class or interface you want to inquire about, but not instance of that class (of course, you can not instance of an interface).

The "Xclass.class.isAssignableFrom(obj.getClass())" equals to the "obj instanceof Xclass". At the compile time, you must know the name of Xclass class and have an instance obj in instanceof operator. In some cases, you don't know the name of the class(es) you wish to test it at compile time, so the isAssignableFrom method is suitable for such cases.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |