This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

q on instanceof operator

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following answers can be legally placed in the place of XXX?
class Check {
Check() { }
}

public class ICheck extends Check {
public static void main ( String[] args) {
Object o = new ICheck();
Check i = new ICheck();
Check c = new Check();

if ( o instanceof XXX) {
System.out.println("True");
}
}
}
A) Object, ICheck only
B) Check , ICheck only
C) Object only
D) Object, Check, ICheck

i answered c, but the correct answer is d. yy?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see whether "aaa instanceof B" is legal, consider the type of aaa. If aaa could possible hold an object that can be cast to type B, then it's legal.

In this case, the class Object variable o is a reference to any object, so it could hold an object of any class you put on the right side of instanceof.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum, I think you are not right Mike. You have to consider the hierarchy of the classes of the object being compared. Variable o is declared as Object, but it holds a reference to a ICheck instance. ICheck extends Check, which extends Object, just like any other java class. Therefore it is a instance of any of these classes, and will passa the instanceof comparation for any of these.
If you compare any object with the class Object (xxx instanceof Object), the comparation will be true, because every object extends Object. But remember, the object in the left MUST BE of the same class or a subclass of the class on the right.
Hope I've made myself clear.
[]s
Pedro Ivo
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested the three possibilities,
o instanceof Object
o instanceof ICheck
o instanceof Check

they all compile OK.

The question didn't ask if the conditional was true, it asked if it was legal, meaning it compiles and excutes without error.
In this case, the three possibilities are also true, since o is an instance of class ICheck and ICheck is a subclass of both Check and Object.
 
Pedro Ivo Dantas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weel, just to make this topic clear: when using the instanceof operator, the left operand can be any java object, while the right hand can be any java class. It will return true if the object is a instance of the class, or any of it subclasses. This relation is also valid for interfaces.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I sound like a language lawyer, but the left side of instanceof is an object reference, not an object. To avoid a compilation error, there must be some class whose instances could both be referenced by the left operand and be cast to the right operand class.

In other words, "a instanceof B" is legal if there is some possible code that would make that expression true. If that is impossible, Java warns you at the compilation stage.

Consider this code:


In "b instanceof A", I could assign a B object to b to make the expression true.

In "a instanceof B", I could assign a B object to a to make the expression true.

In "b instanceof C", there is nothing I could legally assign to b to make the expression true, so the Java compiler warns me that it thinks I made an error.
[ November 19, 2004: Message edited by: Mike Gershman ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about all the edits, the above post is OK now.
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,

If u have two classes,
Class A {}
Class B extends A {}

b instanceof A -> returns true.
a instanceof B -> returns false, right?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"a instanceof B -> returns false, right?"

In your example, You don't have enough information to state the result. The result depends on how a is defined:

A a = new A(); //then if(a instanceof B) --> returns false
A a = new B(); //then if(a instanceof B) --> returns true
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic