• 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: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?!
 
reply
    Bookmark Topic Watch Topic
  • New Topic