<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The strange world of Java autoboxing</title>
	<atom:link href="http://blog.smart-java.nl/blog/index.php/2009/06/19/the-strange-world-of-java-autoboxing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smart-java.nl/blog/index.php/2009/06/19/the-strange-world-of-java-autoboxing/</link>
	<description>Ordina J-Technologies - Java Blog</description>
	<lastBuildDate>Fri, 30 Jul 2010 13:00:28 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Vincent</title>
		<link>http://blog.smart-java.nl/blog/index.php/2009/06/19/the-strange-world-of-java-autoboxing/comment-page-1/#comment-349</link>
		<dc:creator>Vincent</dc:creator>
		<pubDate>Sun, 02 Aug 2009 18:37:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.smart-java.nl/blog/?p=442#comment-349</guid>
		<description>Peter, a boolean that can have 3 values - true, false and null - is not really a boolean, now is it? ;-)

What you should be doing instead of avoiding primitive types, is avoid the use of null completely. Write contracts. Specify invariants, preconditions and postconditions. Almost always there is no good reason to use null values. If you disallow null, code gets a lot simpler. And you safely can - and should - use primitives.

See, for example, JSR 305 (still in progress).

Additionally, design for immutability. Use final members. See Effective Java, 2nd Edition, Item 15 (Minimize mutability).

Sure, primitives in Java are open for debate. There have been lots of discussions about them. That in a true OO-language, you shouldn&#039;t have primitives (Smalltalk, Ruby, Scala). That even without the primitives, Java could be just as fast because of the JIT compiler. And so on. But the fact is that primitives are there. Using them is okay! Especially when you don&#039;t want null values, and do want immutability.

Vincent</description>
		<content:encoded><![CDATA[<p>Peter, a boolean that can have 3 values &#8211; true, false and null &#8211; is not really a boolean, now is it? <img src='http://blog.smart-java.nl/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>What you should be doing instead of avoiding primitive types, is avoid the use of null completely. Write contracts. Specify invariants, preconditions and postconditions. Almost always there is no good reason to use null values. If you disallow null, code gets a lot simpler. And you safely can &#8211; and should &#8211; use primitives.</p>
<p>See, for example, JSR 305 (still in progress).</p>
<p>Additionally, design for immutability. Use final members. See Effective Java, 2nd Edition, Item 15 (Minimize mutability).</p>
<p>Sure, primitives in Java are open for debate. There have been lots of discussions about them. That in a true OO-language, you shouldn&#8217;t have primitives (Smalltalk, Ruby, Scala). That even without the primitives, Java could be just as fast because of the JIT compiler. And so on. But the fact is that primitives are there. Using them is okay! Especially when you don&#8217;t want null values, and do want immutability.</p>
<p>Vincent</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Schuler</title>
		<link>http://blog.smart-java.nl/blog/index.php/2009/06/19/the-strange-world-of-java-autoboxing/comment-page-1/#comment-347</link>
		<dc:creator>Peter Schuler</dc:creator>
		<pubDate>Thu, 30 Jul 2009 10:05:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.smart-java.nl/blog/?p=442#comment-347</guid>
		<description>It&#039;s a big issue that primitives don&#039;t have a null value. Thus they can&#039;t express a &quot;no-set&quot; option. This is especially a problem with booleans. A value of false doens&#039;t mean that the value has explicitly set by the user.

I try to avoid the primitive types as much as possible. Most certainly for class variables.</description>
		<content:encoded><![CDATA[<p>It&#8217;s a big issue that primitives don&#8217;t have a null value. Thus they can&#8217;t express a &#8220;no-set&#8221; option. This is especially a problem with booleans. A value of false doens&#8217;t mean that the value has explicitly set by the user.</p>
<p>I try to avoid the primitive types as much as possible. Most certainly for class variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan-Kees van Andel</title>
		<link>http://blog.smart-java.nl/blog/index.php/2009/06/19/the-strange-world-of-java-autoboxing/comment-page-1/#comment-335</link>
		<dc:creator>Jan-Kees van Andel</dc:creator>
		<pubDate>Sun, 21 Jun 2009 13:26:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.smart-java.nl/blog/?p=442#comment-335</guid>
		<description>There are tons of risks when you start combining primitives and wrappers. For example, check the following code:

&lt;pre lang=&quot;Java5&quot;&gt;
long time = System.currentTimeMillis();
Long total = 0L;
for (int i = 0; i &lt; 1000000000; i++) {
    Integer i2 = i;
    total += i2;
}
System.out.println(&quot;Total: &quot; + total + &quot;, time: &quot; + (System.currentTimeMillis() - time) + &quot;ms&quot;);
&lt;/pre&gt;

Rewrite it to this and performance increases dramatically:

&lt;pre lang=&quot;Java5&quot;&gt;
long total = 0L;
for (int i = 0; i &lt; 1000000000; i++) {
    total += i;
}
System.out.println(&quot;Total: &quot; + total + &quot;, time: &quot; + (System.currentTimeMillis() - time) + &quot;ms&quot;);
&lt;/pre&gt;

This is the output on my machine:
Total: 499999999500000000, time: 23742ms
Total: 499999999500000000, time: 687ms
Total: 499999999500000000, time: 23236ms
Total: 499999999500000000, time: 681ms

Quite a difference, not?

The following is also funny:
&lt;pre lang=&quot;Java5&quot;&gt;
public static Boolean test() {
    return null;
}

public static void main(String[] args) {
    if (test()) { // NPE
        System.out.println(&quot;Yay!&quot;);
    }
}
&lt;/pre&gt;

Luckily, we&#039;ve got tooling to warn us about these mistakes. For example, FindBugs: http://findbugs.sourceforge.net/bugDescriptions.html#NP_BOOLEAN_RETURN_NULL</description>
		<content:encoded><![CDATA[<p>There are tons of risks when you start combining primitives and wrappers. For example, check the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">long</span> time = <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399; font-weight: bold;">Long</span> total = 0L<span style="color: #339933;">;</span>
<span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000000</span><span style="color: #339933;">;</span> i++<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">Integer</span> i2 = i<span style="color: #339933;">;</span>
    total += i2<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Total: &quot;</span> + total + <span style="color: #0000ff;">&quot;, time: &quot;</span> + <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> - time<span style="color: #009900;">&#41;</span> + <span style="color: #0000ff;">&quot;ms&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Rewrite it to this and performance increases dramatically:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">long</span> total = 0L<span style="color: #339933;">;</span>
<span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000000</span><span style="color: #339933;">;</span> i++<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    total += i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Total: &quot;</span> + total + <span style="color: #0000ff;">&quot;, time: &quot;</span> + <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> - time<span style="color: #009900;">&#41;</span> + <span style="color: #0000ff;">&quot;ms&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is the output on my machine:<br />
Total: 499999999500000000, time: 23742ms<br />
Total: 499999999500000000, time: 687ms<br />
Total: 499999999500000000, time: 23236ms<br />
Total: 499999999500000000, time: 681ms</p>
<p>Quite a difference, not?</p>
<p>The following is also funny:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" 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: #003399; font-weight: bold;">Boolean</span> test<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: #006600; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// NPE</span>
        <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Luckily, we&#8217;ve got tooling to warn us about these mistakes. For example, FindBugs: <a href="http://findbugs.sourceforge.net/bugDescriptions.html#NP_BOOLEAN_RETURN_NULL" rel="nofollow">http://findbugs.sourceforge.net/bugDescriptions.html#NP_BOOLEAN_RETURN_NULL</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
