File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InstanceOf operator

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am just confused about instance of operator,i just want to know whether instance of testes about object type or class type
for example
if class A extends class B and we create a object like this

A a=new B();
if we test
if(a instanceof b) it returns true and if we test
if(a instanceof A) then also it returns true
please reply


 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh KumarPandey wrote:
A a=new B();
if we test
if(a instanceof b) it returns true and if we test
if(a instanceof A) then also it returns true
please reply


And you are confused because .... ??? In both cases, it returns true because it is true. You need to, at least, tell us why you think it should not be -- as we don't know where you are confused.

Henry
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of the operator is very descriptive. It does exactly what it says i.e. "Is this object an instance of this type?" It checks where the given object is of some type. Now, type is not necessarily a specific class. I can also be a superclass. For example, you know that a particular instance of a car is a Car. So if car is a variable pointing to an instance of class Car, car instanceof Car will be true. You already know that.

But can you say that a car is an Automobile? If you can say yes, then car instanceof Automobile should also return true.

Can you say that a car is a Fruit? Well, then you know what car instanceof Fruit would return

Apply the same logic to your A and B and you will understand how it works.

HTH,
Paul.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh KumarPandey wrote:i am just confused about instance of operator,i just want to know whether instance of testes about object type or class type
for example
if class A extends class B and we create a object like this

A a=new B();

BTW, if A extends B then this line of code will not compile. May be you mean, B extends A and probably that is your cause of confusion
 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Anilprem thanks for the reply ,i got it
 
Dan Drillich
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

A very interesting perspective about the instanceof operator at Beware of instanceof operator.

Regards,
Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic