aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why instanceof is so uncool 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 "why instanceof is so uncool " Watch "why instanceof is so uncool " New topic
Author

why instanceof is so uncool

Nirmit Dalal
Greenhorn

Joined: Mar 12, 2013
Posts: 22




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

Joined: Mar 10, 2008
Posts: 1763
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 550
    
  11
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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3299
    
    7
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Nirmit Dalal
Greenhorn

Joined: Mar 12, 2013
Posts: 22
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
 
subject: why instanceof is so uncool