• 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

instanceof operator analysis

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the result?
A. w-f
B. f-p w-f
C. w-f b-f
D. f-p w-f b-f
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B


Hello everyone I have understood the code.I'm writting below my understanding please rectify it if necessary

Here 1st lets check is there any possibility of compilation error.

As f is reference of Fish type so it can hold instance of Walleye or Perch
(i.e.,Fish type reference can be assigned to Walleye or Perch object as they lie below it in class hierarchy) and here test is done with respect to Perch so compiler doesn't complain
then in second if expression w is reference of Walleye type so it can hold instance of Walley which is a Fish and here its tested with respect to Fish so compiler doesn't complain
In 3rd case b is reference of BlueGill type it can hold BlueGill instance or its subtype and if its a Fish(i.e.,subtype of Fish) then instaneof check might be true so compiler doesn't complain.

As there is no possibility if compilation error lets move runtime where we will check which if expression gives true and what gives false

Here f holds Walleye object which is a Perch so if it is checked with instanceof Perch then it gives true so f-p is printed
next w holds Walleye object which is a Fish so if its checked with instanceof Fish then it gives true so w-f is printed
then b holds BlueGill object which isn't Fish so if its checked with instanceof Fish then it gives false so b-f isn't printed.


kindly check my understanding and rectify it accordingly


thanks in advance
with regards
Roopam
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roopam Samal wrote:kindly check my understanding and rectify it accordingly


Seems about right. The main thing to remember about instanceof is that it works downwards (in 'hierarchy' terms), so:

x instanceof <Class>

will return true if, and only if, the following conditions are BOTH true:
  • x is NOT null.
  • x's class == <Class>, OR x's class is a subtype (note: NOT a supertype) of <Class>.

  • HIH

    Winston
     
    Marshal
    Posts: 79177
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please always tell us where such questions come from, to avoid copyright problems, etc.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic