Autoboxing making people lazy
When writing in Java5 primitive types get automatically converted to objects if they are needed. This means that you can write code like:
where before you needed
This is nice, and makes code prettier and easier to read.
Unfortunately it makes programmers lazy, and they don't pay attention to if they are using
One problem is that the JVM will be creating and deleting lots of boxing objects all over the place if your code is particularly inconsistent. However, there's a more subtle problem. This code can now NPE at the line marked
map.put(1,4);
where before you needed
map.put(Long.valueOf(1), Long.valueOf(4));
This is nice, and makes code prettier and easier to read.
Unfortunately it makes programmers lazy, and they don't pay attention to if they are using
Long
or long
in methods, and may pass a Long
to a method that takes a long
. Like this:
public void suspicious(Long l) {
foo(l); // <-- Here
}
private void foo(long l) {
System.err.println("Long was: " + l);
}
One problem is that the JVM will be creating and deleting lots of boxing objects all over the place if your code is particularly inconsistent. However, there's a more subtle problem. This code can now NPE at the line marked
Here
, and it's can be difficult to track down.