This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes InstanceOf operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "InstanceOf operator" Watch "InstanceOf operator" New topic
Author

InstanceOf operator

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
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
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3255
    
    2
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3255
    
    2
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

Joined: Sep 08, 2012
Posts: 46
@Paul Anilprem thanks for the reply ,i got it
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1168
Good Day,

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

Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InstanceOf operator
 
Similar Threads
instanceof query
instance of operator
Regarding instanceof operator
instanceof Comparison
instanceof issue