The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 (Not So) Stupid Questions 20: Primitives and Collections
Subject:  Highlighting autoboxing in editor
Date:  2007-11-29 07:12:22
From:  samkass
Response to: Highlighting autoboxing in editor


But it turns a whole class of bugs from a compile-time error to a runtime-error, and is generally a good way to introduce hard-to-debug performance problems.

Consider this misguided code:

Integer i = new Integer(2);
Integer j = new Integer(2);
assertTrue(i >= j); // success
assertTrue(i <= j); // success
assertTrue(i == j); // failure!

In this case, auto-boxing gets invoked for the >= and the <=, and they are compared as ints, since there is no definition for >= with regard to objects. But not for ==, since that operation is defined as pointer-comparison between the objects.

These types of bugs were hard enough to track down without auto-boxing. Any possible benefit auto-boxing brought in coding-time optimizations are more than lost in debug-time problems.

 Feed java.net RSS Feeds