Thursday, March 30, 2017

Difference between instanceof vs isInsatance() in java

instanceOf:-

insatanceOf is an operator in java we can use insataceOf to check whether the given object is of particular type or not and we know at the type at the beginning

Ex: - 

Thread t=new Thread ();
Syso(t instanceOf runnable);
Syso(t instanceOf Object);


isInstance():- 

isInstance() is a method present in java.lang.Class we can use isInstance()  method to check weather the given Object is of particular type or not  and we don’t know the type at the beginning and it is available dynamically at runtime

ex :- 

class Test {
public static viod main(String… args){
Thread t=new Thread();
Syso(Class.forName(args[0]).isInstance(t));
}
}

Java Test Runnable
O/p: true
Java Test String
O/p: false



isIntance() method is method equalent of insanceOf operator.

No comments:

Post a Comment