<?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 for Joe Rawlings</title>
	<atom:link href="http://joerawlings.com/blog/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://joerawlings.com/blog</link>
	<description>Software Engineer</description>
	<lastBuildDate>Sun, 06 Mar 2011 22:26:15 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>Comment on Scala Day 1 Tic-Tac-Toe by Scala Day 2 &#8211; Higher-Order Functions &#124; Joe Rawlings</title>
		<link>http://joerawlings.com/blog/2011/03/scala-day-1-tic-tac-toe/#comment-32</link>
		<dc:creator>Scala Day 2 &#8211; Higher-Order Functions &#124; Joe Rawlings</dc:creator>
		<pubDate>Sun, 06 Mar 2011 22:26:15 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=71#comment-32</guid>
		<description>[...] up on Day 1, here&#039;s day 2 from 7 languages in 7 weeks. These problems are quite a bit more abstract but I [...]</description>
		<content:encoded><![CDATA[<p>[...] up on Day 1, here&#039;s day 2 from 7 languages in 7 weeks. These problems are quite a bit more abstract but I [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 6 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-6/#comment-7</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 16:23:01 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=43#comment-7</guid>
		<description>The answer is (b), 17777 and 43878. This program doesn&#039;t say what you think it does and leading zeros can cause trouble. On line 3, the syntax highlighting gives it away that you are adding 12345 with the long 5432. On line 4, the leading 0 on &#039;01234&#039; makes it an octal literal equal to 1,234 (base 8 ) or 668 in decimal.

The moral is to always use an uppercase el (L) for long literals and never use lowercase el as a variable name. Also, never precede an int literal with 0 unless you want to express it in octal (base 8 )</description>
		<content:encoded><![CDATA[<p>The answer is (b), 17777 and 43878. This program doesn&#8217;t say what you think it does and leading zeros can cause trouble. On line 3, the syntax highlighting gives it away that you are adding 12345 with the long 5432. On line 4, the leading 0 on &#8217;01234&#8242; makes it an octal literal equal to 1,234 (base 8 ) or 668 in decimal.</p>
<p>The moral is to always use an uppercase el (L) for long literals and never use lowercase el as a variable name. Also, never precede an int literal with 0 unless you want to express it in octal (base 8 )</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 5 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-5/#comment-6</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 16:03:02 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=40#comment-6</guid>
		<description>The answer is (a), 5. Computation does overflow (multiplying with ints). 24 * 60 * 60 * 1000 * 1000 &gt; Integer.MAX_VALUE. The simple fix is to multiply with longs.

The moral is to watch out for overflow -- it&#039;s a silent killer. When in doubt, use a larger type.</description>
		<content:encoded><![CDATA[<p>The answer is (a), 5. Computation does overflow (multiplying with ints). 24 * 60 * 60 * 1000 * 1000 > Integer.MAX_VALUE. The simple fix is to multiply with longs.</p>
<p>The moral is to watch out for overflow &#8212; it&#8217;s a silent killer. When in doubt, use a larger type.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 4 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-4/#comment-5</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 15:51:35 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=36#comment-5</guid>
		<description>The answer is (b). Enumerating over entry sets works better for some Map implementations than others. The key line is 15 where the Constructor iterates over entrySet and inserts entries into the set. 2 for the HashMap is pretty obvious but for EnumMap it&#039;s a little trickier. EnumMap is very frugal with its memory usage and actually reuses the same entry object every time. So, when HashSet iterates over the entries it adds the same mutable entry twice and you should never add mutable objects in HashSets.

The fix is pretty painful where by you would need to manually iterate over the entrySet and add your own immutable copies of the entries. The moral here is to take care when iterating over entry sets.</description>
		<content:encoded><![CDATA[<p>The answer is (b). Enumerating over entry sets works better for some Map implementations than others. The key line is 15 where the Constructor iterates over entrySet and inserts entries into the set. 2 for the HashMap is pretty obvious but for EnumMap it&#8217;s a little trickier. EnumMap is very frugal with its memory usage and actually reuses the same entry object every time. So, when HashSet iterates over the entries it adds the same mutable entry twice and you should never add mutable objects in HashSets.</p>
<p>The fix is pretty painful where by you would need to manually iterate over the entrySet and add your own immutable copies of the entries. The moral here is to take care when iterating over entry sets.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 3 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-3/#comment-4</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 15:23:02 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=33#comment-4</guid>
		<description>The answer is (d). This program will run for &gt; 10^15 years before printing anything. This is because the regex exhibits catastrophic backtracking.

Here&#039;s how matcher tests &quot;aaaaa&quot; for &quot;(aa&#124;aab?)+&quot;

&lt;pre&gt;(aa)     (aa)     (aa)    fail
(aa)     (aa)     (aab?)  fail
(aa)     (aab?)   (aa)    fail
(aa)     (aab?)   (aab?)  fail
(aab?)   (aa)     (aa)    fail
(aab?)   (aa)     (aab?)  fail
(aab?)   (aab?)   (aa)    fail
(aab?)   (aab?)   (aab?)  fail&lt;/pre&gt;

This is exponential in length of string! O(2^(n/2)).

Fix this by replacing (aa&#124;aab?)+ with (aa&#124;aab)+. This cuts the spread factor from 1^n to 1^100 and runs the program instantaneously. Moral here is to avoid catastrophic backtracking and sure there&#039;s only one way to match each string. This goes beyond Java and affects most regular expression systems.</description>
		<content:encoded><![CDATA[<p>The answer is (d). This program will run for > 10^15 years before printing anything. This is because the regex exhibits catastrophic backtracking.</p>
<p>Here&#8217;s how matcher tests &#8220;aaaaa&#8221; for &#8220;(aa|aab?)+&#8221;</p>
<pre>(aa)     (aa)     (aa)    fail
(aa)     (aa)     (aab?)  fail
(aa)     (aab?)   (aa)    fail
(aa)     (aab?)   (aab?)  fail
(aab?)   (aa)     (aa)    fail
(aab?)   (aa)     (aab?)  fail
(aab?)   (aab?)   (aa)    fail
(aab?)   (aab?)   (aab?)  fail</pre>
<p>This is exponential in length of string! O(2^(n/2)).</p>
<p>Fix this by replacing (aa|aab?)+ with (aa|aab)+. This cuts the spread factor from 1^n to 1^100 and runs the program instantaneously. Moral here is to avoid catastrophic backtracking and sure there&#8217;s only one way to match each string. This goes beyond Java and affects most regular expression systems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 2 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-2/#comment-3</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 04:41:38 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=19#comment-3</guid>
		<description>The answer is (d). It will actually print out .89999999999999111821580299 and so on.

In this puzzle, the wrong BigDecimal constructor was used. When passing a double in, it is translated to the exact decimal representation of the double&#039;s binary floating-point value. More details in the video.

But, to fix the problem, use the String constructor. new BigDecimal(&quot;2.00&quot;) and new BigDecimal(&quot;1.10&quot;).

Moral is to not use float or double when exact answers are required (like money). Use BigDecimal, int, or long instead.

It&#039;s not the size, it&#039;s how you use it.</description>
		<content:encoded><![CDATA[<p>The answer is (d). It will actually print out .89999999999999111821580299 and so on.</p>
<p>In this puzzle, the wrong BigDecimal constructor was used. When passing a double in, it is translated to the exact decimal representation of the double&#8217;s binary floating-point value. More details in the video.</p>
<p>But, to fix the problem, use the String constructor. new BigDecimal(&#8220;2.00&#8243;) and new BigDecimal(&#8220;1.10&#8243;).</p>
<p>Moral is to not use float or double when exact answers are required (like money). Use BigDecimal, int, or long instead.</p>
<p>It&#8217;s not the size, it&#8217;s how you use it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Java Puzzle 1 by jrawlings</title>
		<link>http://joerawlings.com/blog/2010/11/java-puzzle-1/#comment-2</link>
		<dc:creator>jrawlings</dc:creator>
		<pubDate>Fri, 26 Nov 2010 01:50:26 +0000</pubDate>
		<guid isPermaLink="false">http://joerawlings.com/blog/?p=15#comment-2</guid>
		<description>The correct answer is (c) where the actual exception is a ClassCastException. This is because of raw types and they are dangerous.

You may have noticed the class was typed -- from line 1 Glommer &lt; T &gt; class. Since &lt; T &gt; type is not used anywhere, this makes it a raw type and all generic types referenced are thrown out when compiled. 

This includes throwing out the wildcard type on Collection &lt; ? &gt;, the Integer type on List &lt; Integer &gt;, and the String type on List &lt; String &gt;.

Therefore, the call to glom(strings) using a List will go the the glom method on line 9. This being the most specific, applicable match. Subsequently, the app blows up with a ClassCastException being thrown.

The compiler does help you in this case. When compiled, you will get &quot;Glommer.java uses unchecked or unsafe operations&quot;. So, you can&#039;t say you weren&#039;t warned :)</description>
		<content:encoded><![CDATA[<p>The correct answer is (c) where the actual exception is a ClassCastException. This is because of raw types and they are dangerous.</p>
<p>You may have noticed the class was typed &#8212; from line 1 Glommer < T > class. Since < T > type is not used anywhere, this makes it a raw type and all generic types referenced are thrown out when compiled. </p>
<p>This includes throwing out the wildcard type on Collection < ? >, the Integer type on List < Integer >, and the String type on List < String >.</p>
<p>Therefore, the call to glom(strings) using a List will go the the glom method on line 9. This being the most specific, applicable match. Subsequently, the app blows up with a ClassCastException being thrown.</p>
<p>The compiler does help you in this case. When compiled, you will get &#8220;Glommer.java uses unchecked or unsafe operations&#8221;. So, you can&#8217;t say you weren&#8217;t warned :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

