• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on instanceof operator

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that someone can help straighten me out.... I am tired and running through the chapter questions in the K&B trying to get ready for mock tests and I just hit a snag.

K&B page 285 says, "You can't use the instanceof operator to test across two different class hierarchies."

For some reason, I am not thinking clearly... I know, but it seems as if the only thing that you can test is within the same class hierarchy then you will always get a true.

What am I not seeing correctly?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure ?

System.out.println(new Object() instanceof Thread);
System.out.println(new Thread() instanceof Object);
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at this example:


a instanceof A -> true
a instanceof AA -> false
aa instanceof A -> true
aaa instanceof A -> true
aa instanceof Object -> true (every class is a subclass of Object)
a instanceof BB -> compiler error (incompatibile types)
bb instanceof AA -> compiler error (incompatibile types)
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My eyes are burning, my brain is fried, I've been doing this too long.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

Just refer to page no 112 and 113 of your book.

You can have two extreme situations. First, sub-Class instanceof super-Class, this is always "true", so you will never need to test this. The second situation is when you have two references belonging to different hierarchy like this-class instanceof not-related-with-this-class. Here also, the result will always be the same, but this time "false". So, you will never need to test this case. If you do try to test this, you will get a compile error, which tell you that there is a problem with the logic.

The only situation the instanceof operator is useful when you test, a Super-Class-Reference instanceof Sub-Class-Reference. Here Super-Reference can refer to a Sub-Class object. So you test something like

if (super class reference in fact contains a subclass object) {
// do some specific subclass thing...
}
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rancher ,

pay attention

class A{}
class SubA{}

class B{}
class SubB{}

SubA a = new SubA();
SubB b = new SubB();

if(a instanceof SubA) // true
if(a instanceof A) // true

if(b instanceof SubB) // true
if(b instanceof B) // true

if(a instanceof SubB) // compile time error
if(a instanceof B) // compile time error

why .. Because compiler knows it very well that variable of type SubA
can never never be able to hold the reference of object of B or SubB .
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I have slept, I feel better, this is starting to sink back in. I thank you all for your effort. Somehow, I know that you understand.

One last thing, in the following example


public void exampleMethod(MyClass myObj) {
if (myObj instanceof MyClass) {
System.out.println("Life is good!");
}
else {
System.out.println("Wrong type passed in");
}
}



This would seem to be a good use for instanceof. BUT, will it cause a runtime exception or anything if someone calls it by casting a Frog object to a MyClass? exampleMethod((MyClass)myFrog)); Or will it simply return false?

I know, I know, take the code I just wrote and go test it. And I can, and I will.

I just wanted you to know how bad it has gotten over here!!!

Thanks again folks!

Bob

[ June 24, 2008: Message edited by: Bob Ruth ]

what do you use for a tab in QUOTES? When I hit tab nothing happens and spaces are peeled out?
[ June 24, 2008: Message edited by: Bob Ruth ]
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know, I know, take the code I just wrote and go test it. And I can, and I will.



What did you get?

I got a Uncompilable source code - inconvertible types at:

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What confuses me about instanceof, is that you can test whether a HashMap object is an instanceof Collection, even though Map and Collection are two separate hierarchies and don't seem to share any interfaces as far as i can tell.

Does the JVM make an exception in this case because it knows that Maps and Collections are both part of the Collections framework?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andr� Roodt wrote:What confuses me about instanceof, is that you can test whether a HashMap object is an instanceof Collection, even though Map and Collection are two separate hierarchies and don't seem to share any interfaces as far as i can tell.
Does the JVM make an exception in this case because it knows that Maps and Collections are both part of the Collections framework?


This is not really true. If you declare a reference variable as HashMap, you get a compiler error. But if you declare it as a Map, you don't. I think that's because all interfaces belong to the same hierarchy tree. But I didn't find any link for that.

reply
    Bookmark Topic Watch Topic
  • New Topic