<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Why YOU should use Integer.valueOf(int)</title>
	<link>http://tech.puredanger.com/2007/02/01/valueof/</link>
	<description>Alex Miller's technical blog</description>
	<pubDate>Sun, 12 Oct 2008 17:44:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Illia</title>
		<link>http://tech.puredanger.com/2007/02/01/valueof/#comment-82358</link>
		<pubDate>Wed, 03 Sep 2008 14:13:33 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/02/01/valueof/#comment-82358</guid>
					<description>I tested not in range (actually most of the data we have are such). I used for cycle variable of the same type (int for Integer, long for Long)

new Integer
avg = 11 ns
Integer valueof
avg = 13 ns

you say 2 nanoseconds?
13/11 = 1.18, neraly 20% or 1/5 longer

Long is different (you can see source code why)

new Long
avg = 12 ns

Long.valueof 
avg = 20 ns

now nearly twice longer</description>
		<content:encoded><![CDATA[<p>I tested not in range (actually most of the data we have are such). I used for cycle variable of the same type (int for Integer, long for Long)</p>
<p>new Integer<br />
avg = 11 ns<br />
Integer valueof<br />
avg = 13 ns</p>
<p>you say 2 nanoseconds?<br />
13/11 = 1.18, neraly 20% or 1/5 longer</p>
<p>Long is different (you can see source code why)</p>
<p>new Long<br />
avg = 12 ns</p>
<p>Long.valueof<br />
avg = 20 ns</p>
<p>now nearly twice longer
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Use .intValue() or .equals() &#171; public static overflowing main</title>
		<link>http://tech.puredanger.com/2007/02/01/valueof/#comment-16089</link>
		<pubDate>Sat, 01 Dec 2007 16:26:17 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/02/01/valueof/#comment-16089</guid>
					<description>[...] Later I came across this blog post. When I completed reading it I could not agree more with my mentors line of reasoning. [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Later I came across this blog post. When I completed reading it I could not agree more with my mentors line of reasoning. [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Adam Goucher &#187; Blog Archive &#187; PMD, IntegerInstantiation and a Red Herring</title>
		<link>http://tech.puredanger.com/2007/02/01/valueof/#comment-6332</link>
		<pubDate>Fri, 17 Aug 2007 12:13:04 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/02/01/valueof/#comment-6332</guid>
					<description>[...] Yesterday I did a bit of exploring how well PMD&amp;#8217;s Eclipse plug-in integrates into our development tools / process. Even if you are not planning on implementing static analysis and you are a java shop, you owe it to yourself to become familiar with the rules that PMD and other such tools utilize.  Why? Because while we have in technology a great tradition of reinventing the wheel it is generally not too efficient and managers tend to frown on it too. So why do you want your group to re-learn in their context what others have learned in similar ones?  You will also learn more about the underlying technology of the product you are testing. That is certainly not a bad thing either.  Back to the original point. The code I work against is triggering a pretty sizable amount of hits to the IntegerInstantiation rule which they summarize as IntegerInstantiation: In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly. I can&amp;#8217;t argue with the whole &amp;#8216;memory friendly&amp;#8217; aspect, but we&amp;#8217;re using Java 1.4 which makes this a false positive (something you learn to live with in the world of static analysis.  But through the process I extended my knowledge of how Java works; initially from the description of the rule, and further through articles I found like this which is absolutely a Good Thing&amp;#8482;. [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Yesterday I did a bit of exploring how well PMD&#8217;s Eclipse plug-in integrates into our development tools / process. Even if you are not planning on implementing static analysis and you are a java shop, you owe it to yourself to become familiar with the rules that PMD and other such tools utilize.  Why? Because while we have in technology a great tradition of reinventing the wheel it is generally not too efficient and managers tend to frown on it too. So why do you want your group to re-learn in their context what others have learned in similar ones?  You will also learn more about the underlying technology of the product you are testing. That is certainly not a bad thing either.  Back to the original point. The code I work against is triggering a pretty sizable amount of hits to the IntegerInstantiation rule which they summarize as IntegerInstantiation: In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly. I can&#8217;t argue with the whole &#8216;memory friendly&#8217; aspect, but we&#8217;re using Java 1.4 which makes this a false positive (something you learn to live with in the world of static analysis.  But through the process I extended my knowledge of how Java works; initially from the description of the rule, and further through articles I found like this which is absolutely a Good Thing&trade;. [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Pure Danger Tech &#187; Blog Archive &#187; One more look at Double-Checked Locking</title>
		<link>http://tech.puredanger.com/2007/02/01/valueof/#comment-4241</link>
		<pubDate>Fri, 15 Jun 2007 18:32:58 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/02/01/valueof/#comment-4241</guid>
					<description>[...] I&amp;#8217;ve blogged before on one instance where this is used in the JVM itself. Integer.valueOf() lazily caches Integer objects from -128 to 127 (as required by the JVM specification). But, you don&amp;#8217;t want to incur the cost of creating the cache unless it&amp;#8217;s needed, so the cache is initialized on demand via a private static class. [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] I&#8217;ve blogged before on one instance where this is used in the JVM itself. Integer.valueOf() lazily caches Integer objects from -128 to 127 (as required by the JVM specification). But, you don&#8217;t want to incur the cost of creating the cache unless it&#8217;s needed, so the cache is initialized on demand via a private static class. [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Alex</title>
		<link>http://tech.puredanger.com/2007/02/01/valueof/#comment-627</link>
		<pubDate>Sun, 04 Feb 2007 04:10:03 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/02/01/valueof/#comment-627</guid>
					<description>Tim, good catch on the first point - had a copy/paste error there.  I've re-run and updated the numbers (not much difference really).  

Second, I based my original recommendation on the JDK javadocs which say &quot;If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.&quot;  I still think this is good advice if you heed the first clause regarding whether a new Integer is required (see the prior WeakHashMap comment).  

Also, I did run a quick test to look at the difference if you are requesting a value in the cached range vs out of the cached range:
  
new (regardless): 15 ns
valueOf (in range): 7 ns
valueOf (not in range): 16 ns

So, if you always use valueOf and you request a value out of the cached range, you are costing yourself a nanosecond each time.  It's hard to see how 1 ns (or even an 8 ns savings will help you that much), but as I've said above, I think the benefit is more in the space savings than the speed.  And the larger point is that using a factory method allows the implementation to better optimize the implementation (better caching, native calls, magic JDK hooks, who knows what) in the future without you changing your own code.</description>
		<content:encoded><![CDATA[<p>Tim, good catch on the first point - had a copy/paste error there.  I&#8217;ve re-run and updated the numbers (not much difference really).  </p>
<p>Second, I based my original recommendation on the JDK javadocs which say &#8220;If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.&#8221;  I still think this is good advice if you heed the first clause regarding whether a new Integer is required (see the prior WeakHashMap comment).  </p>
<p>Also, I did run a quick test to look at the difference if you are requesting a value in the cached range vs out of the cached range:</p>
<p>new (regardless): 15 ns<br />
valueOf (in range): 7 ns<br />
valueOf (not in range): 16 ns</p>
<p>So, if you always use valueOf and you request a value out of the cached range, you are costing yourself a nanosecond each time.  It&#8217;s hard to see how 1 ns (or even an 8 ns savings will help you that much), but as I&#8217;ve said above, I think the benefit is more in the space savings than the speed.  And the larger point is that using a factory method allows the implementation to better optimize the implementation (better caching, native calls, magic JDK hooks, who knows what) in the future without you changing your own code.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
