Thinking Outside the Box

Although I have been touting the new autoboxing and unboxing feature in Java 2 version 5, I didn't realize how far the language is willing to go with these automatic conversions.

The following code doesn't work in version 4:

public void setDeposit(Float deposit) {
  if (deposit >= 0) {
    this.deposit = deposit;
  }
}

You can't use a comparison operator like ">" to compare a Float object and a float value -- an "incompatible types" error indicates that you have made Bytecode Jesus cry.

Instead, you have to call the object's floatValue() method:

public void setDeposit(Float deposit) {
  if (deposit.floatValue() >= 0) {
    this.deposit = deposit;
  }
}

In Java 2 version 5, you can write statements that treat objects as if they were the corresponding primitive types, so the original method compiles successfully.

I prefer a statically typed language like Java over a dynamic one like PHP because of the dumb mistakes that are caught. I thought it was heresy when Hans Nowak suggested recently that these highly praised error-catching capabilities are self-fulfilling:

Rigid languages make types important, so whenever you get a type "wrong" (i.e. different from the declared type signature), it's a bug, and the compiler balks loudly. No wonder a lot of these bugs are caught; the language sets the programmer up to make them.

Now that Java has become smart enough to remove a bunch of my dumb errors, my opinion on this issue is no longer static.

Comments

I dont like the new Features in 1.5. I think that are all unneded options to make the Sources unreadable.

Yours
Michael

Add a Comment

All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).