File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes WTF of the day: Java 1.4 compiler bug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "WTF of the day: Java 1.4 compiler bug" Watch "WTF of the day: Java 1.4 compiler bug" New topic
Author

WTF of the day: Java 1.4 compiler bug

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Having to work with code bases running on old JVMs is unpleasant at the best of times, but running into old compiler bugs that are not reproducible on current JDKs is just nasty. Case in point is this code which compiles fine under Java 5:

Using the Java 1.4 compiler causes an "incompatible types for ?: neither is a subtype of the other" error, though. To fix it, we have to help out the compiler with a bit of DWIM:

I myI = true ? ((I) new A()) : ((I) new B());

I really have to make a push to get this code running on a newer JVM...
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I no longer have access to a 1.4 compiler but I do remember running into this many years ago. The solution I remember was to explicitly caste each item. i.e.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Yep, that's the one I found, too (see my first post :-)

BTW, it didn't make a difference if I was a class that both A and B extended - same error.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Lester Burnham wrote:Yep, that's the one I found, too (see my first post :-)

BTW, it didn't make a difference if I was a class that both A and B extended - same error.


Please ignore my post - once again I am guilty of not reading right to the bottom of a posting.

I'm surprised it does not work. Looks like you are going to have to use an explicit if-then-else construct. If that fails to compile then maybe a static method to hide the casting. If that fails then ...
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It's highly discouraged to use the ternary operator in combination with different classes as result. For example this still fails on java version "1.6.0_17" (the latest version on my pc):



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Wouter Oet wrote:It's highly discouraged to use the ternary operator in combination with different classes as result.

Very tempting, though, as a poor man's factory class replacement as long as there are only two possible options :-)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

(Something went horribly wrong).

The first and only time I encountered this bug I must have read the code about 10.000 times and when I finally found the bug I tested it on 2 different machines just to be sure. Later that month I read an article about it I believe it was an example of the Java Puzzlers book.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Wouter Oet wrote:It's highly discouraged to use the ternary operator in combination with different classes as result. For example this still fails on java version "1.6.0_17" (the latest version on my pc):



I think that has to do with auto-(un)boxing. It seems like, for some reason, the Integer and Double are unboxed again, then boxed to the highest type (Double). If I replace the Integer.valueOf(5) with new BigDecimal("5") then I get 5. That's because BigDecimal cannot be unboxed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: WTF of the day: Java 1.4 compiler bug