This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof operator

 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class aa{
public static void main(String argv[]){
aa a = new aa();
bb b=new bb();
System.out.println(b instanceof a);
}
}
class bb extends aa{}


can someone tell me why the following code fails compilation saying class a cannot be found ?

public class aa{
public static void main(String argv[]){
aa a = new aa();
bb b=new bb();
System.out.println(a instanceof b);
}
}
class bb extends aa{}

when this code compiles and prints false why the first one wont compile ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both do not compile. Perhaps you should reread the description of instanceof, especially the part about the second operand.
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry Gaunt for your kind information the second code compiles and runs fine and prints false
Please check it
Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps your code is working under following scenario:



Anyways it is "informatively" funny.


Thanks,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shyam kumarK:
Hi Barry Gaunt for your kind information the second code compiles and runs fine and prints false
Please check it
Thanks,


I did.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Chandra. You must have a class definition in your CLASSPATH named b which is an extension of bb.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
I agree with Chandra. You must have a class definition in your CLASSPATH named b which is an extension of bb.


Or any interface b.
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keith linn was absolutely correct
it was my stupidity to check if instanceof works with rhs being a reference variable ?!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic