• 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

Using instanceof operator

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am writing a mock exam, in that i came over with this question on instanceof operator,
The question is as follows:

Which of the following statements are true?
1.The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2.The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class.
3.The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up in the inheritance chain.
4. The instanceof operator can be used to determine if one reference is of the same class as another reference thus.

The answer given is 2. Can any one explain me why?
Thanks in advance.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instanceof operator tests whether its first operand is an instance of its second. op1 instanceof op2

op1 must be the name of an object and op2 must be the name of a class. An object is considered to be an instance of a class if that object directly or indirectly descends from that class.

Hope the following lines from JLS be of some help to u.

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (�15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishnu priya parimi:
The answer given is 2. Can any one explain me why?



Which one do you think could be true, too, and why? Is there any reason you doubt that 2 could apply?

BTW, I think the answers are worded a little bit sloppily: a reference is not an instance of a class, it references ("points to") an instance of a class - and that instance is checked by the operator.

That's important because the reference has a type, too (the one you declare in the source code), but that is not checked by instanceof.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the following statements are true?



1.The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.



It's incorrect because "The instanceof operator can be used to determine if a reference is an instance of a class and interface"

Example





Result
True
True


2.The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class.



Sure. you can do it. Because primitive wrapper class is a class.

Example
 
reply
    Bookmark Topic Watch Topic
  • New Topic