• 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

Doubt from Dan's test

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color c1 = new Red();
Color c2 = new Blue();
Red r1 = new Red();
boolean b1 = c1 instanceof Color; //line 1
boolean b2 = c1 instanceof Blue; //line2
//boolean b3 = r1 instanceof Blue;
System.out.print(b1+","+b2);
}
}
Above programme prints true false.Shouldn't line 2 give compiler error?I am confused.Actually left hand side operand of instanceof operator should be reference type or object type?For ex in the line c1 instanceof Color c1 reffers to "Color" or "Red"...Hope I make some sense.
Please anybody explain.
Veena
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Veena,
The best way to look at the instance operator is when object referred to by the variable reference on the left side of the instanceof (in this case c1) passes the IS-A test for the class on the right side.
---------------------------
I will expand with sample code. I hope this code will help you.
class A {}
class B extends A {}
class C extends B {
public static void main(String [] args)
{
C c1 = new C();
boolean b1 = c1 instanceof C; // true
boolean b2 = c1 instanceof B; // true
boolean b3 = c1 instanceof A; // true
boolean b4 = c1 instanceof Object; // All classes implicitly extend Object, so true
System.out.println(b1+","+b2+","+b3+","+b4);
}
}
[ July 22, 2003: Message edited by: Alex Radomski ]
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena, this is the way I see it:

the reference type of c1 and c2 is Color. So, as Alex said, is something like
boolean b1 = Color is a Color? True. But
boolean b2 = Color is a Blue?

Blue is a Color, Color is a Color, Red is a Color, but Color is not Blue (wrong direction). Therefore, false.
So far so good. Now, if you uncomment this line:
boolean b3 = r1 instanceof Blue;
you'll be saying:
boolean b3 = Red is a Blue?
hmm.. and checking our hierarchy structure again:

You see that saying "red is a blue" does not belong to the same hierarchy structure, so a compiler error is generated.
Hope it helps
[ July 22, 2003: Message edited by: Andres Gonzalez ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 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.


[ July 22, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color c1 = new Red();
Color c2 = new Blue();
Red r1 = new Red();
boolean b1 = c1 instanceof Color; //line 1
boolean b2 = c1 instanceof Blue; //line2
//boolean b3 = r1 instanceof Blue;
System.out.print(b1+","+b2);
}
}
Above programme prints true false.Shouldn't line 2 give compiler error?I am confused.Actually left hand side operand of instanceof operator should be reference type or object type?For ex in the line c1 instanceof Color c1 reffers to "Color" or "Red"...Hope I make some sense.
Please anybody explain.
Veena


Hi Veena,
The instanceof operator behaves differently between compile time and runtime.
To pass compilation, the reference (1st operand) must be assignable to the 2nd operand. Assignable means that you can cast the reference implicitly or explicitly to the type of the 2nd operand.
In short, if you can do this between the 2 operands, then you will pass compilation:

In line 2 in the code above, c1, which is of type Color, can be assigned to type Blue(by explicit cast).

This is why the compilation did not fail.

Now during runtime, in order to return TRUE, the object pointed to by the reference must be assignable to the 2nd operand w/o an explicit cast. In short, you must be able to do this between the 2 operands for it to return TRUE:

Obviously, line 2 returned FALSE because you cannot do this:

Hope this helps
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.Thank you very much everybody.
Veena
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic