<?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>JAW Speak &#187; puzzle</title>
	<atom:link href="http://jawspeak.com/category/puzzle/feed/" rel="self" type="application/rss+xml" />
	<link>http://jawspeak.com</link>
	<description>Jonathan Andrew Wolter</description>
	<lastBuildDate>Fri, 30 Jul 2010 20:39:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Can you spot Java Puzzler in this snippet?</title>
		<link>http://jawspeak.com/2009/09/30/can-you-spot-java-puzzler-in-this-snippet/</link>
		<comments>http://jawspeak.com/2009/09/30/can-you-spot-java-puzzler-in-this-snippet/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 19:51:17 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=125</guid>
		<description><![CDATA[Reading time: 2 &#8211; 2 minutes

			
				
			
		
I ran across this last week. It was marvelous when we saw what was happening, but entirely puzzling at first.

Boolean someFlag = complicatedLogicToFigureOutFlag&#40;&#41;;
Person person = new Person&#40;someFlag&#41;;

Any signs for concern? How about if Person&#8217;s constructor is:

Person&#40;boolean someFlag&#41; &#123;
    this.someFlag = someFlag;
&#125;

Any warning signs?
Will it compile?
Read more for [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 2 &#8211; 2 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F09%2F30%2Fcan-you-spot-java-puzzler-in-this-snippet%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F09%2F30%2Fcan-you-spot-java-puzzler-in-this-snippet%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I ran across this last week. It was marvelous when we saw what was happening, but entirely puzzling at first.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Boolean</span> someFlag <span style="color: #339933;">=</span> complicatedLogicToFigureOutFlag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>someFlag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Any signs for concern? How about if Person&#8217;s constructor is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> someFlag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">someFlag</span> <span style="color: #339933;">=</span> someFlag<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Any warning signs?</p>
<p>Will it compile?</p>
<p>Read more for the full puzzler.</p>
<p><span id="more-125"></span></p>
<p>If you didn&#8217;t see a warning sign, but said it would compile &#8212; you&#8217;re half right. It will compile, but there&#8217;s a great big NullPointer waiting for you depending on the value of someFlag. Because autoboxing was a shim initially slid into Java 1.5, this will compile:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Boolean</span> someFlag <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>someFlag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will compile, but then NPE ambiguously</span></pre></div></div>

<p>But at runtime, it will fail ambiguously with a Null Pointer on the line of the constructor.</p>
<p>Of course, this will not compile:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// won't compile because signature is boolean</span></pre></div></div>

<p>My pair and I saw this in a test with a particularly ugly constructor with 12 parameters. We were following <a href="http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/">Misko</a>&#8217;s style of &#8220;use nulls in tests, but not in production code&#8221; and thus that Boolean was ending up as null. By binary search we identified which parameter caused the trouble, initialized it to false instead of null, and then shared our &#8220;Ah Ha!&#8221; moment with several other engineers around the office.</p>
<p>Should something like this be an interview question you ask a candidate? Maybe if they are doing great on all other fronts, and you&#8217;re curious how well they understand the intricacies of Java. But I don&#8217;t generally like obscure fact questions because in real life we just figure them out. So, yes I would use this in an interview where we are pairing, so I could watch the candidate troubleshoot.</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/09/30/can-you-spot-java-puzzler-in-this-snippet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
