• 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

 
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 not able to understand the correct usage of instanceof operator.

consider the following example:

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}}

I understand that instanceof operator is used to determine whether particular object is instance of particular class or not.

But i get doubts when i am doing the mock exams.
Can anyone tell me how to use them.

Thanks in advance.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...the output would be
true,false,false

Red object refered by color1 IS A Color object
Red object refered by color1 is not a Blue object
Red object refered by color2 is not a Blue object
 
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:
But i get doubts when i am doing the mock exams.



What kind of doubts?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

The result will be compile-time error.

Suppose the comparison is as below:

RelationalExpression instanceof ReferenceType

According to JLS,

"If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error."

In your example, at line

boolean b3 = color2 instanceof Blue;

color2 is a reference type to Red whereas you are comparing with Blue and the casting to Blue from Red is a compile time error. So is the result.
[ October 06, 2004: Message edited by: Srikanth Reddy ]
 
Ranch Hand
Posts: 1055
  • 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:
I understand that instanceof operator is used to determine whether particular object is instance of particular class or not. But i get doubts when i am doing the mock exams.



Barry said: "If in doubt then compile it and run it." Very useful advice. If you think you got the idea, write some code that tests different variations of the idea. If everything runs smoothly, your doubts should go away.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic