• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

instanceoff

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
JQ+ :
956597418360
=============================================
(obj instanceof C && !(obj instanceof D) )
correctly identifies whether the object referred to by obj was created by instantiating class
C rather than classes A, B and D?
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
Answer :True .
==============================================
(obj instanceof C) - i can see its true ,
!(obj instanceof D) - i can see its true ,
but how come obj in not an instance form A and D classes ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that I quite understand what you're asking. The key to the instanceof operator is to realize that it will return true if the object in question is of the same type or a descendent type of the class given. Therefore, in your example, if you had an object of type C, this would be your following results:

Therefore, if you wanted to check to see if a given object was of type C only, you'd have to do the following:

Check out the JLS, §15.20.2 Type Comparison Operator instanceof for more information.
I hope that helps,
Corey
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if we create
A obj = new C();
then
(obj instanceof A) - true ! ???

please help
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right, because, according to the JLS, this is what the instanceof operator does:


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.


Therefore, if the object that is used is the exact type it is compared to, or a descendent of that type, it returns true.
Corey
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all ,
so it means that JQ+ question 956597418360 is not in the correct form or it's my mistake ?
please help.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The question and answer looks fine to me. Given the inheritance structure, the given test will correctly determine if an object is of type C.
Corey
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh - thank you Corey McGlone.
I was reading this question wrong .
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic