<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>JavaAntiPatterns</title>
	<atom:link href="http://javaantipatterns.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://javaantipatterns.wordpress.com</link>
	<description>Collection of bad coding practices</description>
	<lastBuildDate>Sun, 12 May 2013 20:21:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='javaantipatterns.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>JavaAntiPatterns</title>
		<link>http://javaantipatterns.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://javaantipatterns.wordpress.com/osd.xml" title="JavaAntiPatterns" />
	<atom:link rel='hub' href='http://javaantipatterns.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Measure time intervals using System.currentTimeMillis()</title>
		<link>http://javaantipatterns.wordpress.com/2012/03/15/measure-time-intervals-using-system-currenttimemillis/</link>
		<comments>http://javaantipatterns.wordpress.com/2012/03/15/measure-time-intervals-using-system-currenttimemillis/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 04:13:34 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Threads]]></category>
		<category><![CDATA[timers]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/?p=37</guid>
		<description><![CDATA[Well, what&#8217;s wrong with a common practice of measuring elapsed time in the code like this: (bad) The approach is based on an intuitive assumption that the values returned by currentTimeMillis() (in general, the current system time expressed as a number of milliseconds since midnight, January 1, 1970 UTC) grow smoothly and always are greater [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=37&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Well, what&#8217;s wrong with a common practice of measuring elapsed time in the code like this:</p>
<p>(<b>bad</b>)</p>
<pre class="brush: java; title: ; notranslate">
long startTime = System.currentTimeMillis();
// ...
long elapsedTime = System.currentTimeMillis() - startTime;
</pre>
<p>The approach is based on an intuitive assumption that the values returned by <code>currentTimeMillis()</code> (in general, the current system time expressed as a number of milliseconds since midnight, January 1, 1970 UTC) grow smoothly and always are greater in the future than in the past. This is not always the case.</p>
<p>A simple example when this assumption fails is adjusting the system clock. On the most of the modern systems, correction of the clock is performed automatically by polling a value from the NTP server in a specific period of time. If this correction happens in the middle of your operation, the measured time interval will be wrong or even negative. Note that the typical computer clocks  have an error in tens or even hundreds of milliseconds per hour that means that the correction offset will be up to few seconds if it is performed once a day. </p>
<p>It is safer to use the <b><code>System.nanoTime()</code></b> method which does not depend on a system clock, so it&#8217;s guaranteed that its value grows smoothly from the past to the future:</p>
<p>(<b>good</b>)</p>
<pre class="brush: java; title: ; notranslate">
long startTime = System.nanoTime();
// ...
long elapsedTime = (System.nanoTime() - startTime) / 1000000; // ms
</pre>
<p>And remember that the <code>nanoTime</code> values has nothing to do with any real absolute time, so don&#8217;t try to use it for the Date objects, for instance.</p>
<p>See also: <a href="https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks" title="Inside the Hotspot VM: Clocks, Timers and Scheduling Events" target="_blank">Inside the Hotspot VM: Clocks, Timers and Scheduling Events</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=37&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2012/03/15/measure-time-intervals-using-system-currenttimemillis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Null returned from a method returning a collection or an array</title>
		<link>http://javaantipatterns.wordpress.com/2009/11/11/null-returned-from-a-method-returning-a-collection-or-an-array/</link>
		<comments>http://javaantipatterns.wordpress.com/2009/11/11/null-returned-from-a-method-returning-a-collection-or-an-array/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 17:10:10 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Collections]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[null]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/?p=22</guid>
		<description><![CDATA[Null should never be returned from a method returning a collection or an array. Instead return a empty array (a static final empty array) or one of the empty collections (e.g. Collections.EMPTY_LIST assuming the client should not be modifying the collection). (submitted by Rand McNeely)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=22&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Null should never be returned from a method returning a collection or an array.  Instead return a empty array (a static final empty array) or one of the empty collections (e.g. <code>Collections.EMPTY_LIST</code> assuming the client should not be modifying the collection).</p>
<p>(submitted by Rand McNeely)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=22&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2009/11/11/null-returned-from-a-method-returning-a-collection-or-an-array/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Unneccessary thread safety of StringBuffer</title>
		<link>http://javaantipatterns.wordpress.com/2008/08/22/unneccessary-thread-safety-of-stringbuffer/</link>
		<comments>http://javaantipatterns.wordpress.com/2008/08/22/unneccessary-thread-safety-of-stringbuffer/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 04:07:04 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Strings]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/?p=18</guid>
		<description><![CDATA[Use StringBuilder rather then StringBuffer, unless synchronization is required. StringBuilder is not thread safe, and therefore avoids the synchronization overhead of StringBuffer. (submitted by Robert J. Walker)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=18&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Use <code>StringBuilder</code> rather then <code>StringBuffer</code>, unless synchronization is required. <code>StringBuilder</code> is not thread safe, and therefore avoids the synchronization overhead of <code>StringBuffer</code>.</p>
<p>(submitted by Robert J. Walker)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=18&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2008/08/22/unneccessary-thread-safety-of-stringbuffer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Comparing URLs with &#8216;URL.equals()&#8217;</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/24/comparing-urls-with-urlequals/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/24/comparing-urls-with-urlequals/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 06:53:35 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/24/comparing-urls-with-urlequals/</guid>
		<description><![CDATA[Implementation of equals() in java.net.URL is based on a fancy rule saying that URLs of two hosts are equal as long as they are resolving to the same IP address. It is known to be incompatible with virtual hosting and should not be used. For instance, URL.equals() would consider the URL of this blog (http://javaantipatterns.wordpress.com) [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=17&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Implementation of <code>equals()</code> in <code>java.net.URL</code> is based on a fancy rule saying that URLs of two hosts are equal as long as they are resolving to the same IP address. It is known to be incompatible with virtual hosting and should not be used.</p>
<p>For instance, <code>URL.equals()</code> would consider the URL of this blog (<a href="http://javaantipatterns.wordpress.com">http://javaantipatterns.wordpress.com</a>) to be equal to an URL of any random blog hosted on WordPress &#8211; that is obviously not true.</p>
<p>It is recommended to use java.net.URI objects instead.</p>
<p>P.S.<br />
<code>java.net.URL</code> class is an absolute champion in a number of implemented antipatterns of <code>equals()</code> (and <code>hashCode()</code> as well):</p>
<ul>
<li>It lies</li>
<li>It is <a href="http://javaantipatterns.wordpress.com/2007/11/24/equals-and-hashcode-are-context-sensitive/">incostistent</a></li>
<li>It is <a href="http://javaantipatterns.wordpress.com/2007/11/22/using-urls-in-collections/">slow and breakable</a></li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=17&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/24/comparing-urls-with-urlequals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;equals()&#8217; and &#8216;hashCode()&#8217;  are context-sensitive</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/24/equals-and-hashcode-are-context-sensitive/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/24/equals-and-hashcode-are-context-sensitive/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 06:20:43 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hashcode]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/24/equals-and-hashcode-are-context-sensitive/</guid>
		<description><![CDATA[equals() and hashCode() implementations should rely only on an internal object state. Making them dependent on other objects, context of use and other external conditions conflicts with the general contracts of consistency: &#8220;For any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false&#8221; [*] &#8220;Whenever it is invoked [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=16&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><code>equals()</code> and <code>hashCode()</code> implementations should rely only on an internal object state. Making them dependent on other objects, context of use and other external conditions conflicts with the general contracts of consistency:</p>
<blockquote><p>&#8220;For any reference values <code>x</code> and <code>y</code>, multiple invocations of <code>x.equals(y)</code> consistently return <code>true</code> or consistently return <code>false</code>&#8221; [<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#equals(java.lang.Object)">*</a>]</p></blockquote>
<blockquote><p>&#8220;Whenever it is invoked on the same object more than once during an execution of a Java application, the <code>hashCode</code> method must consistently return the same integer&#8221; [<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#hashCode()">*</a>]</p></blockquote>
<p>Violating those contracts may cause all kinds of weird and unpredictable behavior.</p>
<p>A known example of this antipattern is <code>equals()</code> and <code>hashCode()</code> in <code>java.net.URL</code> implementation where they are depending on information returned by a domain name server, rather than on actual stored URL data.</p>
<p>See also:</p>
<ul>
<li><a href="http://javaantipatterns.wordpress.com/2007/11/22/using-urls-in-collections/">Using URLs in Collections</a></li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=16&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/24/equals-and-hashcode-are-context-sensitive/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Not taking advantage of &#8216;toString()&#8217;</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/22/not-taking-advantage-of-tostring/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/22/not-taking-advantage-of-tostring/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 23:16:50 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[Strings]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/22/not-taking-advantage-of-tostring/</guid>
		<description><![CDATA[Overriding toString() method gives you a cheap way to provide human-readable labels for objects in output. When the objects are used in GUI containers, such as JLists or JTables, it allows to use the default models and renderers instead of writing the custom ones. Bad: Good:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=15&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Overriding <code>toString()</code> method gives you a cheap way to provide human-readable labels for objects in output. When the objects are used in GUI containers, such as <code>JList</code>s or <code>JTable</code>s,  it allows to use the default models and renderers instead of writing the custom ones.</p>
<p><span id="more-15"></span></p>
<p>Bad:</p>
<pre class="brush: java; title: ; notranslate">
System.out.println(&quot;Author: &quot;+person.getName());
</pre>
<p>Good:</p>
<pre class="brush: java; title: ; notranslate">
class Person {
    ...
    public String toString() {
        return this.getName();
    }
}
...
System.out.println(&quot;Author: &quot;+person);
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=15&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/22/not-taking-advantage-of-tostring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Instatiation of immutable objects</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/22/instatiation-of-immutable-objects/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/22/instatiation-of-immutable-objects/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 15:14:42 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/22/instatiation-of-immutable-objects/</guid>
		<description><![CDATA[Creating new instances of immutable primitive type wrappers (such as Number subclasses and Booleans) wastes the memory and time needed for allocating new objects. Static valueOf() method works much faster than a constructor and saves the memory, as it caches frequently used instances. It is guaranteed that two Boolean instances and Integers between -128 and [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=12&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Creating new instances of immutable primitive type wrappers (such as <code>Number</code> subclasses and <code>Boolean</code>s) wastes the memory and time needed for allocating new objects. Static <code>valueOf()</code> method works much faster than a constructor and saves the memory, as it caches frequently used instances.</p>
<p>It is guaranteed that two <code>Boolean</code> instances and <code>Integer</code>s between -128 and 127 to be pre-cached, thus you definitely should not use the constructor to instantiate them.</p>
<p><span id="more-12"></span></p>
<p><strong>Bad:</strong></p>
<pre class="brush: java; title: ; notranslate">
Integer i = new Integer(0);
Boolean b = new Boolean(true);
</pre>
<p><strong>Good:</strong></p>
<pre class="brush: java; title: ; notranslate">
Integer i = Integer.valueOf(0);
Boolean b = Boolean.valueOf(true); // or Boolean.TRUE
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=12&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/22/instatiation-of-immutable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>Unbuffered I/O</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/22/unbuffered-io/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/22/unbuffered-io/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 14:41:04 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[I/O]]></category>
		<category><![CDATA[io stream buffer native]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/22/unbuffered-io/</guid>
		<description><![CDATA[Reading and writing I/O streams byte-by-byte is too expensive, as every read()/write() call refers to the underlying native (JNI) I/O subsystem. Usage of buffered streams reduces the number of native calls and improves I/O performance considerably. Bad: Good:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=11&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Reading and writing I/O streams byte-by-byte is too expensive, as every <code>read()</code>/<code>write()</code> call refers to the underlying native (JNI) I/O subsystem. Usage of buffered streams reduces the number of native calls and improves I/O performance considerably.</p>
<p><span id="more-11"></span></p>
<p><strong>Bad:</strong></p>
<pre class="brush: java; title: ; notranslate">
InputStream in = new FileInputStream(f); 
int b; 
while ((b = in.read()) != -1) {    
    ... 
}   
</pre>
<p><strong>Good:</strong></p>
<pre class="brush: java; title: ; notranslate">
InputStream in = new BufferedInputStream(new FileInputStream(f));
...
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=11&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/22/unbuffered-io/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;equals()&#8217; does not check for null argument</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/22/equals-does-not-check-for-null-argument/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/22/equals-does-not-check-for-null-argument/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 14:08:35 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/22/equals-does-not-check-for-null-argument/</guid>
		<description><![CDATA[If you override equals() method in your class, always check if an argument is null. If a null value is passed, equals() must unconditionally return false (no NullPointerException should be thrown!). Bad: Good:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=10&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you override <code>equals()</code> method in your class, always check if an argument is <strong>null</strong>. If a null value is passed, <code>equals()</code> must unconditionally return <code>false</code> (no <code>NullPointerException</code> should be thrown!).</p>
<p><span id="more-10"></span></p>
<p><strong>Bad:</strong></p>
<pre class="brush: java; title: ; notranslate">
class Person {
    ...
    public boolean equals(Object o) {
        return (o instanceof Person) &amp;&amp; 
               this.getName().equals(((Person)o).getName());
    }
    ...
}
</pre>
<p><strong>Good:</strong></p>
<pre class="brush: java; title: ; notranslate">
class Person {
    ...
    public boolean equals(Object o) {
        return (o != null) &amp;&amp; 
               (o instanceof Person) &amp;&amp; 
               this.getName().equals(((Person)o).getName());
    }
    ...
}
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=10&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/22/equals-does-not-check-for-null-argument/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;compareTo()&#8217; is incompatible with &#8216;equals()&#8217;</title>
		<link>http://javaantipatterns.wordpress.com/2007/11/22/compareto-is-incompatible-with-equals/</link>
		<comments>http://javaantipatterns.wordpress.com/2007/11/22/compareto-is-incompatible-with-equals/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 13:53:39 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[compareto]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://javaantipatterns.wordpress.com/2007/11/22/compareto-is-incompatible-with-equals/</guid>
		<description><![CDATA[If a class implements Comparable, compareTo() method must return zero if and only if equals() returns true for the same non-null argument (and vice versa). Violating this rule may cause unexpected behavior of the objects. See also: ‘equals()’ is overriden while ‘hashCode()’ is not<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=9&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If a class implements <code>Comparable</code>, <code>compareTo()</code> method must return zero if and only if <code>equals()</code> returns <code>true</code> for the same non-null argument (and vice versa). Violating this rule may cause unexpected behavior of the objects.</p>
<p><span id="more-9"></span></p>
<pre class="brush: java; title: ; notranslate">
class Product implements Comparable
    ...
    public int compareTo(Object o) {
        if (o == null)
            throw new NullPointerException();
        if (this.equals(o))
            return 0;
        ...    
    }
}
</pre>
<p>See also:</p>
<ul>
<li><a href="http://javaantipatterns.wordpress.com/2007/11/22/equals-is-overriden-while-hashcode-is-not/">‘equals()’ is overriden while ‘hashCode()’ is not</a></li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/javaantipatterns.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/javaantipatterns.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaantipatterns.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaantipatterns.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaantipatterns.wordpress.com&#038;blog=2172170&#038;post=9&#038;subd=javaantipatterns&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaantipatterns.wordpress.com/2007/11/22/compareto-is-incompatible-with-equals/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/95a247ad953123c27fadba1718e5bbb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyberborean</media:title>
		</media:content>
	</item>
	</channel>
</rss>
