Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

how does instanceof operator work?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried executing the following code:
1)class foo{
}
2)class bar{
}
3)public class Testinst{
4)public static void main(String [] a){
5)foo f= new foo();
6)System.out.println(f instanceof foo);
7)System.out.println(f instanceof bar);
}
}
I thought the answer has to be true and false. But I got a compile time error saying "impossible for foo to be an instance of bar".

I know the question is simple and changing the second line to 'class bar extends foo' would prevent the error, but it got me thinking as to how instanceof operator works. Can someone please explain why did I get a compile error or how the instanceof operator works?.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instanceof operator is used to check if an object reference belongs to a certain type or not.
The type can either be an interface or a superclass.


The use of instanceof in java is as follows

Explanation-------

(op1 instanceof op2) ....where op1 is a reference of any object & op1 is any class name.



>>> During compile time the JVM will check whether the op1 is of type op2 or not.If it is not of same type then it will through compile time exception;
During runtime it will return true only if the Object which is referenced by op1 is not null.

I hope this will help.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,
Following are the constraints of the instanceof operator :
1.The operands must be a reference variable(LHS) and a class name(RHS).
2.The reference variable must be related to the class name in some way, that is, they should be inside the same inheritance hierarchy.

If any of the above conditions is not satisfied, you get a compiler error.

Note : LHS - Left hand side of instanceof operator
RHS - Reft hand side of instanceof operator

Following is my definition for instance operator :
"The instanceof operator returns true if the reference variable in the LHS is an object of the class name in RHS or an object of a subclass of that class name"

You get the compile error in your program due to the failure of the condition no.2 - that is, the classes foo and bar were not related to each other.

God is great. Friendship is double great.
Mathavan
SCJP 1.4
SCWCD (preparing)
 
Swapna Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got it now.
Thank you.
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also check my comment
"During runtime it will return true only if the Object which is referenced by op1 is not null."

Check the example....
--------------------

class foo{
}
class bar{
}
public class Testinst{
static foo f;
public static void main(String [] a){
System.out.println(f instanceof foo);

}
}
 
Swapna Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks a lot for specifying it.
 
Message for you sir! I think it is a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!