Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why instanceof is so uncool

 
Nirmit Dalal
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




This is question from Enthuware . i cant figure out why this code wont compile
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't compile, because the compiler has all the information necessary to determine at compile-time that any object referenced by b is always of the type Bat or a subtype of Bat. It also knows that the types Bat and Bird are not in the same type hierarchy. Therefore no object referenced by b can ever be an instance of Bird.
 
Piet Souris
Ranch Hand
Posts: 1099
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, but why is this considered an error and won't compile?
The error being about unconvertible types.

If you replace the last if by:

then NetBeans gives me two warnings:

braces must be used in if - else constructions
the branche is never used

but it compiles and runs as expected.

Strange.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Indeed, but why is this considered an error and won't compile?
The error being about unconvertible types.


Simply.... because the Java Language Specification says so. As mentioned, if Java detects that the instanceof comparison always returns false (at compile time), it is an error. Now, as for why the language was designed that way, unfortunately, I don't think there is any documentation of that -- certainly not in the JLS.

Piet Souris wrote:
If you replace the last if by:

then NetBeans gives me two warnings:

braces must be used in if - else constructions
the branche is never used

but it compiles and runs as expected.


First, this is a completely different section of the JLS -- this is discussed in the "unreachable code" section -- so arguably, not sure how it is related.

Anyway, interestingly, the JLS does explain why this works. The "unreachable code" checks are purposely relaxed for "if" conditionals, and the reason for this, is to allow for conditional compilation. You can actually confirm this by changing the "if" to a "while", and it should fail to compile.

Henry

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3669
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirmit Dalal wrote:



This is question from Enthuware . i cant figure out why this code wont compile


For others, who do not have access to the question bank, there is detailed explanation provided with the question ( QID: 2.910 ):

b points to an object of class Bat, which does not extend from Bird. Now, it is possible for b to point to an object of any subclass of Bat. However, it is not possible for that subclass to extend Bird (because a class can at most extend from only one class). Therefore, it is not possible for b to point to an object of a class that extends Bird. The compiler figures out this fact at compile time itself and so the code fails to compile.

Note that there is no compilation issue with b instanceof Flyer because Flyer is an interface and it is possible for b to point to an object of a class that is a sub class of Bat and also implements Flyer. So the compiler doesn't complain. If you make Bat class as final, b instanceof Flyer will not compile because the compiler knows that it is not possible for b to point to an object of a class that implements Flyer.


HTH,
Paul.
 
Nirmit Dalal
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So have i got it correct if i say that instanceof operator is just used with classes who are linked with some inheritance design and results in error otherwise?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic