Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
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 ?


SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Both do not compile. Perhaps you should reread the description of instanceof, especially the part about the second operand.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
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

Joined: Feb 28, 2007
Posts: 1707
Perhaps your code is working under following scenario:



Anyways it is "informatively" funny.


Thanks,


cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: May 06, 2007
Posts: 30
keith linn was absolutely correct
it was my stupidity to check if instanceof works with rhs being a reference variable ?!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator