• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

tricky question

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess this should explain:

www.visionwebhosting.net

Regards,
Mehul
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it will an Runtime error as in runtime itself we will know what is the actual instance.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code gives a compile time error as instanceOf is a Java Keyword.
The name of the class cannot be a keyword.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the class name instanceOf. Actual keyword is instanceof (lowercase o)
 
Mukesh Mittal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Santosh. Its a compile time error not runtime error.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic