The Artima Developer Community
Sponsored Link

Doing Things with Java that Should Not Be Possible
Nasty Little JDK 1.5 Compiler Bug
by Heinz Kabutz
October 12, 2004
Summary
There is a serious error in the JDK 1.5 compiler, pointed out to me by one of my readers, Amotz Anner. Using final local variables in conjunction with ternary if-else where one side of the values is null, leads to incorrect results.

Advertisement

There is a serious error in the JDK 1.5 compiler, pointed out to me by one of my readers (of The Java Specialists' Newsletter), Amotz Anner.

Amotz has submitted a bug report already to Sun, and as soon as we have a number from Sun (may take up to three weeks) we will post that in a follow-up.

Here is the problem:

  final String name = false ? "Heinz" : null;
  System.out.println(name);

When compiled using the JDK 1.5 compiler, it will print out "Heinz". It does not matter whether you say -source 1.[234] or whether you run it using any of the JDK 1.[2345].x JVMs from Sun.

This happens only when the local variable is set to "final" and one of the values of the ternary if-else is literal null.

PLEASE let's not make this into a mud-slinging match against Sun Microsystems, ok? They are overloaded with bug reports as it is. The reason that I post this is to make you aware to NOT use the final local variables in combination with ternary if-else.

Talk Back!

Have an opinion? Readers have already posted 1 comment about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Heinz Kabutz adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Heinz Kabutz enjoys driving Java to the limits, and then a bit beyond. He has been programming in Java since 1997 on several very unimportant projects. During that time, he has picked up some horrifying tips on how you can get the most out of Java. These are published on his bi-monthly "The Java(tm) Specialists' Newsletter" (http://www.javaspecialists.co.za). It is not for the uninitiated :-) Heinz received a PhD in Computer Science from the University of CapeTown. He loves living in South Africa as it is both beautiful and interesting. Professionally, Heinz survives by writing Java code, insulting, ahem, consulting, and presenting courses on Java and Design Patterns.

This weblog entry is Copyright © 2004 Heinz Kabutz. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us