• 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

Questions regarding instanceof

 
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm going through OCA part of Kathy and Bert's latest OCA Java SE 7 book and realised the following doesn't quite make any sense to me.

Question 1 - why will that be a compilation error? I tried putting "d instance Cat" into a if condition and it returns false as expected but not a compilation error, am i missing something?

"You can't use the instanceof operator to test across two different class hierarchies."



"Compilation fails - there's no way d could ever refer to a Cat or a subtype of Cat.".



Question 2 - what's the difference between Foo[] and Foo[1], what is the book trying to imply?

Question 3 - when I run instanceof Foo[] f = new Foo[1] against Bar, I get a compilation error (incompatible conditional operand types), so why is the table in the book stated that it will return false ?




A sample of the table provided:

1. Foo instance instanceof [Foo, Bar, Face, Object] -> will return true
2. Foo [] instanceof [Foo, Bar, Face] -> false
3. Foo [] instanceof [Object] -> true
4. Foo [ 1 ] instanceof [Foo, Bar, Face, Object] -> true




Let me know if you need further information. Thanks in advance!

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vince,

First of all, a warm welcome to CodeRanch!

Vince Tan wrote:Question 1 - why will that be a compilation error? I tried putting "d instance Cat" into a if condition and it returns false as expected but not a compilation error, am i missing something?


Can you show me your code with the if condition? Because if you try to use the instanceof operator on two classes of different hierarchies, you'll always get a compilation error. And that makes sense: if 2 classes A and B are not related to each other (like Cat and Dog, Integer and String,...) it's simply impossible that an object of class A could be an instance of class B (because they are unrelated). There is no way, a Plane could be an instance of Meat or Plant. That's why you'll get a compiler error if you try to do.

Vince Tan wrote:Question 2 - what's the difference between Foo[] and Foo[1], what is the book trying to imply?
Question 3 - when I run instanceof Foo[] f = new Foo[1] against Bar, I get a compilation error (incompatible conditional operand types), so why is the table in the book stated that it will return false ?


I put a small code snippet together:


So Foo[] refers to an array of type Foo and Foo[1] refers to an element in an array of type Foo. And you are correct: the table contains an error. This line Foo [] instanceof [Foo, Bar, Face] -> false should be Foo [] instanceof [Foo, Bar, Face] -> compiler error. Well spotted. I'll add it to the errata overview thread.

Hope it helps!
Kind regards,
Roel
 
Vince Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for the quick response!

My code is actually very simple









Btw, could you clarify what it means by different hierarchies, does it have to be on a different package entirely?

Would be good if you could give me a couple of examples on when it will be a compilation error instead of returning a false. For example, based on my understanding at runtime when using instanceof on a Cat to a Dog I expect it to return false instead of a compiler error, unless the whole point of instanceof is actually for generics?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Tan wrote:My code is actually very simple


Your code gives me (as expected) a compiler error on both instanceof statements (Incompatible conditional operand types A and B).

Vince Tan wrote:Btw, could you clarify what it means by different hierarchies, does it have to be on a different package entirely?


No, different class hierarchies has nothing to do with packages at all, it's about relationships (inheritance) between 2 classes. In short, a superclass and subclass belong to the same class hierarchy; but 2 sibling classes belong to different class hierarchies. An example to illustrate:
So based on this example, these statements are all true:
  • Cat, Animal and Object belong to same class hierarchy
  • Cat and Dog are siblings (both extending Animal), so Cat and Dog belong to different class hierarchies
  • Poodle, Dog, Animal and Object belong to same class hierarchy
  • Animal and Car are siblings (both extending Object), so Animal and Car belong to different class hierarchies

  • To summarize: if you can say TypeX IS-A TypeY, then TypeX and TypeY both belong to same class hierarchy. For example (based on the example above): Dog IS-A Animal, so Dog and Animal belong to same class hierarchy. Poodle IS-NOT-A Cat, Poodle and Cat belong to different class hierarchies.

    Vince Tan wrote:Would be good if you could give me a couple of examples on when it will be a compilation error instead of returning a false. For example, based on my understanding at runtime when using instanceof on a Cat to a Dog I expect it to return false instead of a compiler error, unless the whole point of instanceof is actually for generics?


    The instanceof operator is not used for generics. The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

    No problem! I can provide a couple of examples (again all based on the example above).
    These will all return true:

    These will all return false:

    And these will result in a compiler error (not because the syntax is wrong, but because we compare types of different class hierarchies):


    Hope it helps!
    Kind regards,
    Roel
     
    Vince Tan
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Got it now! Thanks so much for your help and time!
     
    Ranch Hand
    Posts: 603
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Hi All
    Excellent Explanation by Roel De Nijs
    Regards,
    Deepak Lal
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:And you are correct: the table contains an error. This line Foo [] instanceof [Foo, Bar, Face] -> false should be Foo [] instanceof [Foo, Bar, Face] -> compiler error. Well spotted.


    Added to the errata overview.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am happy with the errata update but what caught me out was the 1st reference to Foo [] in the table. Based on the precursor code

    Foo [] does not appear anywhere. Are we supposed to assume that Foo [] is an array of Foo objects ? Should this definition not be supplied in the pre-cursor code or is there something more fundamental that I am missing ?

    Cheers
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paddy O Riley wrote:Foo [] does not appear anywhere. Are we supposed to assume that Foo [] is an array of Foo objects ?


    What else could it be if mentioned in a Java study guide? I'm pretty sure Foo[] can't be anything else but a Foo array declaration.

    Paddy O Riley wrote:Should this definition not be supplied in the pre-cursor code or is there something more fundamental that I am missing ?


    No, it shouldn't and you are definitely not missing something more fundamental. Just adding Foo[] to the pre-cursor code (class hierarchy with classes and interface) wouldn't compile at all. The pre-cursor code just lists the class hierarchy which is used (and referred to) in table 4.1
     
    What's wrong? Where are you going? Stop! Read this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic