dimanche mars 14, 2010

Hey Mark, we missed the point here !

I was recently watching Mark Reinhold's interview on Oracle Technology Network. Mark is Principal Engineer at Sun Oracle and works on OpenJDK and future Java SE releases.

The interview went over many different subjects around Java SE : projects coin (simplification), jigsaw (modularity), closures (...)  

Mark was also insisting on the many -ities of the Java platform : security, availability, scalability, readability, ...

One of my favorite is readability ! It is important to understand how a piece of code is expected to behave just by reading it.  This helps to reduce bugs when we write code and / or to easily catch bugs when reading code written by our peers.

Java SE 5, released about 5 years ago, introduced a feature called auto boxing, allowing to mix and match Java's primitive types (such as int, boolean, ...) with their Object counterpart (such as Integer, Boolean, ...)

The objective of autoboxing is to simplify code and increase readability and, as such, is a welcome addition. But the devil lies into the details.

The example of code below shows incoherent behavior introduced by auto boxing : 

Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2); //true

Integer integer3 = 128;
Integer integer4 = 128;
System.out.println(integer3 == integer4); //false

You read it right : the first equality is true, while the second is false.

I would expect either both expressions to be false (Object references never must be compared for equality, my grand mother learned me) Or both expressions to be true (should auto boxing fullfil its promises). 

This weird behavior is caused by some compiler optimizations happening behind the scene.  When the compiler is auto boxing values between -127 and 127, it always reuse the same Object instance.  In our example, integer1 and integer2 are therefore references to the same Integer object, hence the first equality. 

For values outside of this (-127, 127) range, the compiler creates different Object instances for each reference, even when the values are identical.

We all learned that we should not use object references when comparing values, but auto boxing encourages us to write such code and when doing so, we end up with incoherent behaviors such as the one described above, which can make bugs very difficult to catch.

Hey Mark, if readability is really a concern : we missed the point with auto boxing !  

About

S├ębastien Stormacq is a Senior Software Architect at Sun Microsystems. He uses his 15+ years of professional experience to design large scale, secured and highly transactional architectures based on Sun's middleware solutions.

Search

Archives
« avril 2014
lun.mar.mer.jeu.ven.sam.dim.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today