Sunday 27 May 2018

instanceof vs isInstance()

'isInstance()' method, instanceof operation are used to check whether the specified object is assignment-compatible with the object represented by this Class. It is the dynamic equivalent of 'instanceof' operator.

By using 'isInstance()', instanceof operator, you can check whether the object can be casted to other type or not.

For example, let’s see the definition of Integer class, it looks like below.



public final class Integer extends Number implements Comparable<Integer> {

}

As you see Integer is a subclass of Number, Comparable, and Object (Object is the super class for all the classes). Now if you define, an integer variable, you can assign it to Number, Comparable and Object types. You can check this compatibility using 'isInstance' method.

Test.java

package com.sample.test;

public class Test {

 public static void main(String args[]) {

  Integer var1 = new Integer(10);

  System.out.println("Can I assign var1 to Integer : " + Integer.class.isInstance(var1));
  System.out.println("Can I assign var1 to Number : " + Number.class.isInstance(var1));
  System.out.println("Can I assign var1 to Comparable : " + Comparable.class.isInstance(var1));
  System.out.println("Can I assign var1 to Object : " + Object.class.isInstance(var1));

  System.out.println("\nCan I assign var1 to Double : " + Double.class.isInstance(var1));
  System.out.println("Can I assign var1 to Float : " + Float.class.isInstance(var1));

  System.out.println("\nvar1 instanceof Integer : " + (var1 instanceof Integer));
  System.out.println("var1 instanceof Number : " + (var1 instanceof Number));
  System.out.println("var1 instanceof Comparable : " + (var1 instanceof Comparable));
  System.out.println("var1 instanceof Object : " + (var1 instanceof Object));
 }
}

Output

Can I assign var1 to Integer : true
Can I assign var1 to Number : true
Can I assign var1 to Comparable : true
Can I assign var1 to Object : true

Can I assign var1 to Double : false
Can I assign var1 to Float : false

var1 instanceof Integer : true
var1 instanceof Number : true
var1 instanceof Comparable : true
var1 instanceof Object : true

instanceof vs isInstance()
If you want to check the compatible class of given object at run time, then you should use isIntance() method.

Find the below working application.

Test.java

package com.sample.test;

public class Test {

 /**
  * Return true, if we can assign object to the class represented by
  * <code>className</clode>
  * 
  * @param className
  * @param object
  * @return
  */
 private static boolean isCompatible(String className, Object object) {
  try {
   return Class.forName(className).isInstance(object);
  } catch (ClassNotFoundException e) {
   throw new RuntimeException("Class not found " + className + "," + e);
  }
 }

 public static void main(String args[]) {

  Integer var1 = new Integer(10);

  System.out.println("is var1 comptible with Integer? : " + isCompatible("java.lang.Integer", var1));
  System.out.println("is var1 comptible with Number? : " + isCompatible("java.lang.Number", var1));
  System.out.println("is var1 comptible with Comparable? : " + isCompatible("java.lang.Comparable", var1));

  System.out.println("\nis var1 comptible with Float? : " + isCompatible("java.lang.Float", var1));
  System.out.println("is var1 comptible with Boolean? : " + isCompatible("java.lang.Boolean", var1));
 }
}

Output

is var1 comptible with Integer? : true
is var1 comptible with Number? : true
is var1 comptible with Comparable? : true

is var1 comptible with Float? : false
is var1 comptible with Boolean? : false



Previous                                                 Next                                                 Home

No comments:

Post a Comment