<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CertPal &#187; autobox</title>
	<atom:link href="http://www.certpal.com/blogs/tag/autobox/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Tue, 24 Aug 2010 17:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Autoboxing / Unboxing gotchas</title>
		<link>http://www.certpal.com/blogs/2009/08/autoboxing-unboxing-gotchas/</link>
		<comments>http://www.certpal.com/blogs/2009/08/autoboxing-unboxing-gotchas/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 16:33:38 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[autobox]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[unbox]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=108</guid>
		<description><![CDATA[An intro to the autoboxing and unboxing gotchas that you can encounter in java]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fautoboxing-unboxing-gotchas%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fautoboxing-unboxing-gotchas%2F" height="61" width="51" /></a></div><p>Java introduced the concept of autoboxing and unboxing since JDK 5. It has been used quite liberally by developers ever since. But those that do not understand the difference between primitives and Wrapper types can end up misusing it. They can especially become dangerous when coupled with a framework that provides some sort of type mapping, such as entity EJBs that map Wrappers to table columns.</p>
<p>Let us look at some of the gotchas</p>
<p><strong>Gotcha 1:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
    Test test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> autobox <span style="color: #339933;">=</span> test.<span style="color: #006633;">autobox</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> autobox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What could possibly be the output of this program ? Well, let me not leave you guessing</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Exception</span> in thread <span style="color: #0000ff;">&quot;main&quot;</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">NullPointerException</span>
	at com.<span style="color: #006633;">tests</span>.<span style="color: #006633;">Test</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#40;</span>Test.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>As a java programmer, when I see this exception stack trace I immediately think &#8216;Hmm&#8230; the test reference must have become null somehow&#8217; without realizing that unboxing the null to a primitive failed. I could end up wasting time analyzing something that is perfectly all right, if I do not have a look at the autobox() method first.</p>
<p><strong>Gotcha 2:</strong><br />
Lets write a method that will simply add to a counter</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> loopDeLoop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">long</span> past <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Integer</span> iCounter<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>iCounter<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> iCounter<span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">1000000000</span><span style="color: #339933;">;</span>iCounter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        iCounter<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">long</span> now <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>now <span style="color: #339933;">-</span> past<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The execution time of this method is 8197883698 nanos. Convert the Integer iCounter in line 4 to int iCounter and the execution time drops to 509410924 nanos. That is a 16 fold decrease. The result will be more significant as this number increases.</p>
<p>Now if you are telling me no one is foolish enough to loop 1000000000 times to add a counter, think again. Errors / inefficiencies have a way of <a href="http://www.certpal.com/blogs/2009/08/code-too-large-for-try-statement/" target="_blank">creeping into the system</a>. Some one may not intentionally try to do this. But it can happen.</p>
<p><strong>Gotcha 3:</strong></p>
<p>Think of all the memory you are wasting by creating those extra objects. Everytime you do a ++ on an Integer you are creating a new one. Integers are immutable so you cannot assign a new value to it without actually creating a new object. This adds a silent memory overhead. Another side effect of this is that it makes you think Integer is actually mutable. Its not !</p>
<p><strong>Gotcha 4:</strong></p>
<p>When you map a potentially autoboxable element to say a database row and retrieve it in an unboxed fashion, KABOOM. You get the cryptic NullPointerException stated in Gotcha 1. The twist comes when (if) a framework tries to handle this exception. If the framework writes internal codegen and does not reveal the actual code written, good luck debugging this buddy <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Gotcha 5:</strong></p>
<p>Have a look at this code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Tearable
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> tear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TearBoy <span style="color: #000000; font-weight: bold;">implements</span> Tearable
<span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> tear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What happens when I have a reference to a Tearable interface that points to the concrete TearBoy class ? Nothing. You know why ? This code will not even compile. It looks a little deceptive because you would think that an Integer can by unboxed to int and the code would work. But autoboxing here would break the interface&#8217;s contract.</p>
<p>All that said, there have been times when the autoboxing / unboxing feature has saved some time. But it is easy to overlook the gotchas mentioned above and you could end up wasting time instead. Use autoboxing / unboxing judiciously and think about the side effects before you use it.<br />
<script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2009/08/autoboxing-unboxing-gotchas/';</script><br />
<script type="text/javascript">var dzone_title = 'Autoboxing / Unboxing gotchas';</script><br />
<script type="text/javascript">var dzone_blurb = 'An intro to the autoboxing and unboxing gotchas that you can encounter in java and why you should use it carefully';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script> </p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fautoboxing-unboxing-gotchas%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fautoboxing-unboxing-gotchas%2F" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/autoboxing-unboxing-gotchas/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
