aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes tricky question   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "tricky question   " Watch "tricky question   " New topic
Author

tricky question

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
hai ranchers
can any one explain on this question


1 class instanceOf
2 {
3 public static void main(String arg[])
4 {
5 G g=new G();
6 H h=new H();
7 System.out.println(h instanceof G);
8 }
9 }
10 class G{}
11 class H{}


a Program compiles correctly and prints true when executed.

b Program compiles correctly and prints false when executed.

c Run time error

d Compile time error.

why it is compile time error

by velan vel
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
I guess this should explain:

www.visionwebhosting.net

Regards,
Mehul
santosh kothapalli
Greenhorn

Joined: Dec 27, 2005
Posts: 26
The answer is compile time error as class G and class H has no relationship
hence g instanceof H or h instaceof G results in incompatible types.

cheers,
santosh.


Santosh K<br />SCJP 1.4,SCWCD
Hiren Dutta
Greenhorn

Joined: Dec 30, 2005
Posts: 10
I think it will an Runtime error as in runtime itself we will know what is the actual instance.
Falguni Mehta
Greenhorn

Joined: Dec 30, 2005
Posts: 1
The code gives a compile time error as instanceOf is a Java Keyword.
The name of the class cannot be a keyword.
Mukesh Mittal
Greenhorn

Joined: Dec 29, 2005
Posts: 16
Please check the class name instanceOf. Actual keyword is instanceof (lowercase o)
Mukesh Mittal
Greenhorn

Joined: Dec 29, 2005
Posts: 16
I agree with Santosh. Its a compile time error not runtime error.
Abdi Duale
Greenhorn

Joined: Dec 13, 2005
Posts: 7
Interesting question. So, when say instanceof operator is used to check whether an object is of a particular type what exactly are we checking in order to determine their relationship? Are we checking if they have the same methods, members etc or what?
For instance, in the above example if H was a subclass of G, the test would return true right? because H would have some commonalities of G such as non-private methods, non-private class variable ( basically is would pass the IS-A test). what exactly does intanceof check?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tricky question