Instance Of Operator
Syntax:- t instanceof x
t ==> Object / Obj reference.
x ==> class name / interface
Ex:-
- Thread t = new Thread ( ) ;
- System.out.println ( t instanceof Thread ) ; // true
- System.out.println ( t instanceof Object ) ; // true
- System.out.println ( t instanceof Runnable ) ; // true
- System.out.println ( t instanceof String ) ; // compile time error //incovertible type found Thread, required String
To use instanceof operator some relationship between argument type i.e either parent to child or child to parent otherwise we will get compile time error.
Where ever we are checking parent Object is of child type or not then we will get false as the o/p.
Object o = new Object ( c) ;
System.out.println ( o instanceof String ) ; // false
For any class or interface ‘x’ ” null instanceof x ” is always returns false.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.