« They're in cahoots!
Main
Jen and Steve's Wedding »
Ouch
I've just gotten my first bite mark from primitive auto-boxing in Java 1.5. The following code is meant to read a file in either special mode or non-special mode, or to auto-detect special-ness if the boolean argument is null. This would (correctly) be a compile error in 1.4.2: void readFile(String filename, Boolean isSpecial) {
    if (!isSpecial) // OUCH
        readFile(filename, autodetectSpecial(filename));
    } else {
        readFile(filename, isSpecial.booleanValue());
    }
}
The problem should be that I'm checking "isSpecial" for null without explicitly comparing it to null--- this is a mistake I make when I switch back and forth between Javascript and Java a lot.

But in Java 1.5 this code compiles without complaint because the compiler thinks I meant to auto-dereference the boolean and check its value for falseness. And then at runtime I get a nice shiny NullPointerException when the auto-boxing logic attempts to dereference "isSpecial" and read its value as a boolean.

I'm not saying that auto-boxing is terrible, but it is an example of how it can bite you, and it's not 100% gravy.
Comments
blog comments powered by Disqus
The views expressed on this site are mine personally, and do not necessarily reflect the views of my employer.