• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

clear my doubt????

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
consider the following code,
i thoght
b1,b2,b3 are assigned with values true,false and false respectively,
but the answer given is "compile time error"..
can somebody explain me how that comes???


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);
}}
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to let you know there are "CODE" tags that you can place around your code to keep the formatting so that we can read the code better.

Under the Add Reply button there is a set of buttons, one has the title CODE and it will automatically put the code tag in your post. Then paste your code in between the start and end tags of CODE.

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


hi friends,
consider the following code,
i thoght
b1,b2,b3 are assigned with values true,false and false respectively,
but the answer given is "compile time error"..
can somebody explain me how that comes???


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);
}}



boolean b3 = color2 instanceof Blue;

This line is responsible for the compile-time error.
There's no superclass-subclass relationship between class Red and class Blue. IOW, the 2 types are inconvertible.
 
shameena Hussain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i still have a doubt...
when color2 is not an instanceof blue, b2 shd be assigned with false.
but is it like this,
The two objects having super-sub class relationship can only be compared using 'instanceof'?
clear my doubt

thanks in advance
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This operator confuses a lot
You can try reading;http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#80289

Also Marcus Green says "Note that instanceof tests against a class name and not against an object reference for a class."

I guess it checks at the compile time if the two references are not convertibles to prevent it from reaching runtime & raising an exception.

Would be great if someone else can write something on this.

Thanks
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shameena, did you import java.awt.Color? If you didn't the code would not compile.
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shameena, There is no Color() constructor in the Color Class. The two class you extend color from are using default constuctors. You would have to call one of the Color constructor from these class to make the program work. Please refer to : http://java.sun.com/j2se/1.3/docs/api/java/awt/Color.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic