<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gAcl="http://schemas.google.com/acl/2007" xmlns:sites="http://schemas.google.com/sites/2008" xmlns:gs="http://schemas.google.com/spreadsheets/2006" xmlns:dc="http://purl.org/dc/terms" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan</id><updated>2012-02-07T16:00:58.068Z</updated><title>Posts of Java Tips, Hacks, Updates</title><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan" /><link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan" /><link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/batch" /><generator version="1" uri="http://sites.google.com">Google Sites</generator><openSearch:startIndex>1</openSearch:startIndex><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/JavaTipsHacksAndUpdates" /><feedburner:info uri="javatipshacksandupdates" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry gd:etag="&quot;YD4peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/8668908115533848174</id><published>2012-02-06T10:59:07.963Z</published><updated>2012-02-06T10:59:30.289Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-06T10:59:30.286Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Creating Thread-Safe Singleton?</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr">A singleton class should be designed to ensures that there exists only one instance per application. Special care must be taken if your application is deployed on a clustered environment as in this case it is possible that multiple instance of your singleton class are available in your application.   <br /><br />Here are a few ways to create a thread safe singleton classes in your application. <br /><h3><a name="TOC-1-Lazy-loading-Singleton-instance--" />1) Lazy loading Singleton instance - Using Synchronized method or block</h3><code>public class SingletonClass{</code><br /><code>    private SingletonClass sc;</code><br /><code>    private SingletonClass(){}</code><br /><br /><code>    public static SingletonClass getInstance(){</code><br /><code>        synchronized(SingletonClass.class) {</code><br /><code>              if (sc == null) {</code><br /><code>                  sc = new SingletonClass();</code><br /><code>              } else {</code><br /><code>                 return sc;</code><br /><code>              }</code><br /><code>        }</code><br /><code>    }</code><br /><br /><code>}</code><br /><br />Issues<br />- The use of synchronized keyword in a singleton class means that only one thread will be executing the synchronized block at a time and all other threads would be waiting.<br /><h3><a name="TOC-2-Early-initialization-Singleton---" />2) Early initialization Singleton - Using Static Member Variable</h3><code>public class SingletonClass{</code><br /><code>    private static sc = new SingletonClass();</code><br /><code>    private SingletonClass(){}</code><br /><br /><code>    public static SingletonClass getInstance(){</code><br /><code>        return sc;</code><br /><code>    }</code><br /><code>}</code><br /><br />This approach is also known as early initialization because we are creating the singleton instance at an early stage and not when the instance is actually needed by another class.<br /><br />Issues<br />- The use of static member variable means that this singleton instance will be created as soon as the class is loaded by any Classloader in JVM.  <br /><h3><a name="TOC-3-Singleton-using-Inner-Classes" />3) Singleton using Inner Classes</h3><code>public class SingletonClass{</code><br /><code>    private SingletonClass(){}</code><br /><code>    private static class InstanceHolder{</code><br /><code>        private static final SingletonClass INSTANCE = new SingletonClass(); </code><br /><code>    }</code><br /><code>    public static SingletonClass getInstance(){</code><br /><code>        return InstanceHolder.INSTANCE; //line1</code><br /><code>    }</code><br /><code>}</code><br /><br />Here the instance is being created on demand and is also thread safe. The use of inner classes helps in the sense that the very first time singleton object is requested, the inner class is loaded by line1 and this loading of inner class causes the static member variable to be created and returned. Next time, the singleton member variable is requested causes the same static reference variable to be returned.<br /><h3><a name="TOC-4-Singleton-using-enums" />4) Singleton using enums</h3><code>public enum Singleton{</code><br /><code>    INSTANCE;</code><br /><code>    private int a;</code><br /><code>    public int getA() {</code><br /><code>        return a;</code><br /><code>    }</code><br /><code>}</code><br /><br />To get a single instance of this enum one should use:<br /><code>Singleton.INSTANCE</code><br />To get the value of member variable a, one should use:<br /><code>Singleton.INSTANCE.getA();</code><br /><br />In my experience using enums is the best way to implement Singleton design pattern in any Java application. It is thread safe and also provides lazy initialization.<br /></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/howtocreatethread-safesingleton" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/8668908115533848174" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8668908115533848174" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8668908115533848174" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>howtocreatethread-safesingleton</sites:pageName><sites:revision>2</sites:revision></entry><entry gd:etag="&quot;YDUpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/7143303568223728909</id><published>2011-10-31T06:46:09.501Z</published><updated>2012-01-19T17:03:09.961Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-19T17:00:08.056Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Use PMD - Programming Mistake Detector?</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><h4><a name="TOC-About-PMD" />About PMD</h4>How do you ensure that your code follows standard programming principles? For most Java development projects going on these days the answer would be to use "PMD". It is aiming towards becoming a de-facto tool for analyzing the source code and is being used by more and more Java applications everyday. <br /><br /><b>Note:</b> There are a lot many tools that PMD competes with i.e Checkstyle, 
FindBugs, Hammurapi, Soot, Squale etc. However exploring capabilities of these 
tools(other than PMD) are out of scope of this article.<br /><br />PMD is a static rule set based Java source code analyzer that identifies potential problems in the code like:<br /><ol><li><b>Possible bugs </b>- empty try/catch/finally/switch statements</li><li><b>Dead code </b>- unused local variables, parameters and private methods</li><li><b>Suboptimal code</b> - wasteful String/StringBuffer usage</li><li><b>Overcomplicated expressions</b> - unnecessary if statements, for loops that could be while loops</li><li><b>Duplicate code</b> - copied/pasted code means copied/pasted bugs</li></ol>You can <a href="http://sourceforge.net/project/showfiles.php?group_id=56262" target="_blank">download everything from here</a>, and you can get an overview of all the rules at the <a href="http://pmd.sourceforge.net/rules/index.html" target="_blank">rulesets index page</a>.<br /><br />PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.<br /><br /><h4><a name="TOC-Instructions-for-configuring-PMD-fo" />Instructions for configuring PMD for Eclipse <br /></h4><br />Let me walk you through the steps required to quickly configure it with your Eclipse installation. It works like a breeze.<br /><ol><li>Start Eclipse.</li><li>Start the installation procedure : select the Help&gt;Software Updates&gt;Find and Install... menu item.</li><li>Select "Search for new features to install" option and click Next.</li><li>Click New Remote Site...</li><li>Give a name (ie PMD Eclipse Site), enter the URL http://pmd.sourceforge.net/eclipse</li><li>Select this new site in the Sites to include in search list and click Next.</li><li>Select PMD for Eclipse 3 and Apache Xerces in the "Select the features to install" list and click Next.</li><li>Accept the terms of the license agreements and click Next.</li><li>Verify that the install location is your Eclipse installation directory, otherwise select the correct one, click Finish.</li><li>A warning appear telling the feature is not signed. Ignore and click Install to continue.</li><li>Accept to restart the workbench to load PMD into the workbench.</li></ol>Eclipse is restarted and a PMD welcome page is displayed : the plugin is correctly installed. </div><div dir="ltr"><br /></div><div dir="ltr"><h4><a name="TOC-Writing-custom-rules-suggested-by-D" />Writing custom rules (suggested by David Karr)</h4><div>It is interesting and important to note that you can write your custom rules. <span style="font-family:Verdana,Helvetica,Arial,sans-serif;font-size:small;line-height:16px">Writing PMD rules is cool because you don't have to wait for PMD team to get around to implementing feature requests. Following are the </span>two approaches to write custom rules.</div><div><div><ul><li>Write a rule using Java</li><li>Write an XPath expression</li></ul><div>More details about each of these approaches can be found at <a href="http://pmd.sourceforge.net/howtowritearule.html">http://pmd.sourceforge.net/howtowritearule.html</a>.</div></div></div><div><font face="Verdana, Helvetica, Arial, sans-serif" size="2"><span style="line-height:16px"><br /></span></font></div><br />Happy coding.. <br /><div><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:100px;"><div class="sites-embed-content sites-embed-type-plusone"><div class="g-plusone" data-count="false" data-size="standard" data-source="google:sites" /></div></div></div></div><h4><a name="TOC-Source" />Source</h4><a href="http://pmd.sourceforge.net/">http://pmd.sourceforge.net/</a><br /><br /><div /></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/usepmd-programmingmistakedetector" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/7143303568223728909" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7143303568223728909" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7143303568223728909" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>usepmd-programmingmistakedetector</sites:pageName><sites:revision>9</sites:revision></entry><entry gd:etag="&quot;YDkpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/6204008779508374636</id><published>2011-11-29T16:30:21.682Z</published><updated>2011-11-30T09:41:21.124Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-30T09:41:21.122Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Hot tips on using Eclipse effectively</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div>Following are some tips that shall help you in avoiding potential issues and for being a little more productive while working with eclipse.
<h4><a name="TOC-Avoid-installation-problems" />Avoid installation problems</h4>
<div>Never install a new version of Eclipse on top of an older version. Rename the old one first to move it out of the way, and let the new version be unpacked in a clean directory.</div>
<h4><a name="TOC-Recovering-your-messed-up-workspace" />Recovering your messed up workspace</h4>
<div>Corrupted workspace is a common occurrence and troublemaker for many developers. So If your Eclipse installation has startup errors or a corrupted configuration, it might be time to get a fresh start. Start Eclipse with the –clean option, and all cached framework and runtime data will be cleared out. This often helps fix plug-in issues and improve general stability.</div>
<h4><a name="TOC-Increase-the-memory-allocation" />Increase the memory allocation</h4>
<div>With new plugins getting added to the core eclipse functionality and the need to use additional third party plugins, the memory requirements for your eclipse workspace increases. The default memory allocation configured in eclipse is not enough for most J2ee development projects and that causes a sluggish response from you eclipse. If you get Out of Memory errors or sluggish response, you may have to increase the defaults that are set in eclipse.ini file in the Eclipse installation directory. In particular, if you get an error about “PermGen” memory (permanent generation), add this line at the end and restart Eclipse: -</div>
<div><br />
</div>
<div><code>XX:MaxPermSize=256m </code></div>
<div><br />
</div>
<div>Use the lowest memory settings that work and perform well for your mix of projects.</div>
<h4><a name="TOC-Side-by-side-editing" />Side by side editing</h4>
<div>By dragging editors, you can show two files side by side. You can also edit two portions of the same file by using the Window &gt; New Editor command.</div>
<h4><a name="TOC-Automatic-code-improvements" />Automatic code improvements</h4>
<div>Set up Eclipse to automatically format source code and organize imports on every save. Select Window &gt; Preferences &gt; Java Editor &gt; Save Actions to enable these actions. This dialog also lets you configure actions like removing unnecessary casts or adding missing annotations. Configuring your eclipse with optimized Compiler, Formatter and CheckStyle settings is described in detail in the post <a href="http://www.javagyan.com/useful-tips/usingeclipseeffectively">Using Eclipse Effectively</a></div>
<h4><a name="TOC-Keyboard-shortcuts" />Keyboard shortcuts</h4>
<div>It is productive and convenient to use keyboard shortcuts for performing certain tasks in eclipse rather than looking for options to do the same in various navigation menus. For example looking up references of a variable, method or a class can be quickly achieved via shortcut Ctrl+Shift+g. For your reference I have included a list of the most important keyboard shortcuts in my post <a href="http://www.javagyan.com/useful-tips/EclipseKeyboardShortcuts">Eclipse Keyboard Shortcuts</a></div>
<div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><h4><a name="TOC-Other-related-posts" />Other related posts</h4><div><a href="http://www.javagyan.com/useful-tips/usingeclipseeffectively">Using Eclipse Effectively</a><br /></div><div><a href="http://www.javagyan.com/useful-tips/EclipseKeyboardShortcuts">Eclipse Keyboard Shortcuts</a><br /></div>
<div><a href="http://www.javagyan.com/useful-tips/skipovercertainclasseswhenusingstepintoineclipse%E2%80%99sdebugger">Skip over certain classes when using Step Into(F5) in Eclipse’s debugger</a></div>
<div><a href="http://www.javagyan.com/useful-tips/changedefaultauthornameforjavadocsineclipse">Change Default Author Name for JavaDocs in Eclipse</a><br />
</div>
<div><a href="http://www.javagyan.com/useful-tips/remotedebuggingonjboss">Remote debugging on Jboss</a></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/hottipsoneclipse" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/6204008779508374636" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6204008779508374636" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6204008779508374636" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>hottipsoneclipse</sites:pageName><sites:revision>5</sites:revision></entry><entry gd:etag="&quot;XSl7JmA9&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/4524596595086974198</id><published>2010-12-29T19:26:18.679Z</published><updated>2011-11-30T07:47:55.093Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-30T07:47:55.091Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Package by feature, not layer</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:234px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=234;
google_ad_height=60;
google_ad_format="234x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div>The first question in building an application is "How do I divide it up into packages?". For typical business applications, there seems to be two ways of answering this question.</div><div dir="ltr"><div><ul><li>Package by feature</li><li>Package by layer</li></ul></div><div>The package-by-feature style seems to be the superior of the two<font size="2"> </font><font size="2"><b>(<i><a href="http://www.javagyan.com/articles/packagebyfeaturenotlayer">Read the full article on this topic</a></i>)</b></font> :</div><div><ul><li style="list-style-position:outside;list-style-type:square"><b>Higher Modularity: </b>As mentioned above, only package-by-feature has packages with high cohesion, high modularity, and low coupling between packages.</li><li style="list-style-position:outside;list-style-type:square"><b>Easier Code Navigation:</b> Maintenance programmers need to do a lot less searching for items, since all items needed for a given task are usually in the same directory. Some tools that encourage package-by-layer use package naming conventions to ease the problem of tedious code navigation. However, package-by-feature transcends the need for such conventions in the first place, by greatly reducing the need to navigate between directories.</li><li style="list-style-position:outside;list-style-type:square"><b>Higher Level of Abstraction: </b>Staying at a high level of abstraction is one of programming's guiding principles of lasting value. It makes it easier to think about a problem, and emphasizes fundamental services over implementation details. As a direct benefit of being at a high level of abstraction, the application becomes more self-documenting : the overall size of the application is communicated by the number of packages, and the basic features are communicated by the package names. The fundamental flaw with package-by-layer style, on the other hand, is that it puts implementation details ahead of high level abstractions - which is backwards.</li><li style="list-style-position:outside;list-style-type:square"><b><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div>Separates Both Features and Layers:</b> The package-by-feature style still honors the idea of separating layers, but that separation is implemented using separate classes. The package-by-layer style, on the other hand, implements that separation using both separate classes and separate packages, which does not seem necessary or desirable.</li><li style="list-style-position:outside;list-style-type:square"><b>Minimizes Scope:</b> Minimizing scope is another guiding principle of lasting value. Here, package-by-feature allows some classes to decrease their scope from public to package-private. This is a significant change, and will help to minimize ripple effects. The package-by-layer style, on the other hand, effectively abandons package-private scope, and forces you to implement nearly all items as public. This is a fundamental flaw, since it doesn't allow you to minimize ripple effects by keeping secrets.</li><li style="list-style-position:outside;list-style-type:square"><b>Better Growth Style: </b>In the package-by-feature style, the number of classes within each package remains limited to the items related to a specific feature. If a package becomes too large, it may be refactored in a natural way into two or more packages. The package-by-layer style, on the other hand, is monolithic. As an application grows in size, the number of packages remains roughly the same, while the number of classes in each package will increase without bound.</li></ul></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/packagebyfeaturenotlayer" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/4524596595086974198" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4524596595086974198" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4524596595086974198" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>packagebyfeaturenotlayer</sites:pageName><sites:revision>14</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/4255063452942067552</id><published>2011-08-23T10:41:36.772Z</published><updated>2011-11-30T07:23:13.260Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-30T07:23:13.257Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Choosing the right Collection</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div style="text-align:center" /><div dir="ltr"><span style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">Here is a quick guide for selecting the proper implementation of a 
</span><a href="http://java.sun.com/javase/6/docs/api/java/util/Set.html" style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">Set</a><span style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">,
</span><a href="http://java.sun.com/javase/6/docs/api/java/util/List.html" style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">List</a><span style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">, 
or </span><a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html" style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif">Map</a><span style="line-height:20px;font-size:small;font-family:trebuchet ms,sans-serif"> in your application. </span></div><div dir="ltr"><p style="font-family:trebuchet ms,sans-serif"><font size="2">The best general purpose or 'primary' implementations are likely ArrayList, LinkedHashMap, and
LinkedHashSet. Their overall performance is better, and you should use them
unless you need a special feature provided by another implementation. That
special feature is usually ordering or sorting.

</font></p><p style="font-family:trebuchet ms,sans-serif"><font size="2">Here, "ordering" refers to the order of items returned by an <a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html">Iterator</a>,
and "sorting" refers to sorting items according to <a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html">Comparable</a>
or <a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html">Comparator</a>.
<br /> 
</font></p><table border="1" cellspacing="0" style="font-family:trebuchet ms,sans-serif" width="100%">
<tbody><tr>
<td style="text-align:center"><b><font size="2">Interface</font></b></td>

<td style="text-align:center"><b><font size="2">HasDuplicates?</font></b></td>

<td colspan="5" style="text-align:center"><b><font size="2">Implementations</font></b></td>

<td style="text-align:center"><b><font size="2">Historical</font></b></td>
</tr>

<tr>
<td><font size="2">Set</font></td>

<td><font size="2">no</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/HashSet.html">HashSet</a></font></td>

<td align="center"><font size="2">...</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedHashSet.html">LinkedHashSet</a>*</font></td>

<td align="center"><font size="2">...</font></td>

<td align="center"><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html">TreeSet</a></font></td>

<td>
<center><font size="2">...</font></center>
</td>
</tr>

<tr>
<td><font size="2">List</font></td>

<td><font size="2">yes</font></td>

<td align="center"><font size="2">...</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html">ArrayList</a>*</font></td>

<td align="center"><font size="2">...</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html">LinkedList</a></font></td>

<td>
<center><font size="2">...</font></center>
</td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/Vector.html">Vector</a>,
<a href="http://java.sun.com/javase/6/docs/api/java/util/Stack.html">Stack</a></font></td>
</tr>

<tr>
<td><font size="2">Map</font></td>

<td><font size="2">no duplicate keys </font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/HashMap.html">HashMap</a></font></td>

<td align="center"><font size="2">...</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedHashMap.html">LinkedHashMap</a>*</font></td>

<td align="center"><font size="2">...</font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html">TreeMap</a></font></td>

<td><font size="2"><a href="http://java.sun.com/javase/6/docs/api/java/util/Hashtable.html">Hashtable</a>,
<a href="http://java.sun.com/javase/6/docs/api/java/util/Properties.html">Properties</a></font></td>
</tr>
</tbody></table>

<div><font size="2"><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></font></div><font size="2">Principal features of non-primary implementations :
</font><ul style="font-family:trebuchet ms,sans-serif"><li><font size="2">HashMap has slightly better performance than LinkedHashMap</font>
</li><li><font size="2">HashSet has slightly better performance than LinkedHashSet</font> </li><li><font size="2">
TreeSet is ordered and sorted, but slow</font></li><li><font size="2">
TreeMap is ordered and sorted, but slow</font></li><li><font size="2">
LinkedList has fast adding to the start of the list, and fast
deletion from the interior via iteration</font></li></ul><font size="2"><span style="font-family:trebuchet ms,sans-serif">

Iteration order for above implementations :
</span></font><ul style="font-family:trebuchet ms,sans-serif"><li><font size="2">HashSet - <i>undefined</i></font></li><li><font size="2">HashMap - <i>undefined</i></font></li><li><font size="2">LinkedHashSet - insertion order</font></li><li><font size="2">LinkedHashMap - insertion order of keys (by default), or 'access order'</font></li><li><font size="2">ArrayList - insertion order</font></li><li><font size="2">LinkedList - insertion order</font></li><li><font size="2">TreeSet - ascending order, according to Comparable / Comparator</font></li><li><font size="2">TreeMap - ascending order of keys, according to Comparable / Comparator</font></li></ul><font size="2"><span style="font-family:trebuchet ms,sans-serif">

For LinkedHashSet and LinkedHashMap, the re-insertion
of an item does not affect insertion order.
</span></font><p style="font-family:trebuchet ms,sans-serif"><font size="2">While being used in a Map or Set, these items must
not change state (hence, it is recommended that these items be immutable
objects):
</font></p><ul style="font-family:trebuchet ms,sans-serif"><li><font size="2">
keys of a Map</font></li><li><font size="2">
items in a Set</font></li></ul><font size="2"><span style="font-family:trebuchet ms,sans-serif">
Sorting requires either that :
</span></font><ul style="font-family:trebuchet ms,sans-serif"><li><font size="2">
the stored items implement <a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html">Comparable</a></font></li><li><font size="2">
a <a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html">Comparator</a> 
for the stored objects be defined</font></li></ul><font size="2"><span style="font-family:trebuchet ms,sans-serif">
To retain the order of a ResultSet as specified in an ORDER BY
clause, insert the records into a List or a LinkedHashMap.<br /><div><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div></span></font></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/choosingtherightcollection" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/4255063452942067552" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4255063452942067552" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4255063452942067552" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>choosingtherightcollection</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;UCl7JmA9&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/6610339296030179420</id><published>2011-10-24T06:18:56.951Z</published><updated>2011-11-30T07:20:40.128Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-30T07:20:40.127Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Evolution of Java</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:160px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=160;
google_ad_height=600;
google_ad_format="160x600_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div>I thought it would be useful for architects, designers and
developers to understand how Java has evolved since its inception so that they
are aware of what all capabilities they have access to when working with a
particular version of Java.  Besides, it
is always good to have knowledge about evolution of the technologies that you
are working with. <div style="text-align:center" /><div class="sites-embed-align-left-wrapping-on"><div class="sites-embed-border-on sites-embed" style="width:700px;"><h4 class="sites-embed-title">Evolution of java</h4><div class="sites-embed-object-title" style="display:none;">Evolution of java</div><div class="sites-embed-content sites-embed-type-presently"><iframe src="http://docs.google.com/present/embed?hl=en&amp;id=0AZ6Wf8mnIbkvZGNnMnJjdnhfNDY4YzJ2NXY3ZGo&amp;size=l" width="700" height="559" title="Evolution of java" frameborder="0" id="1096882635" /></div></div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/evolutionofjava" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/6610339296030179420" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6610339296030179420" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6610339296030179420" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>evolutionofjava</sites:pageName><sites:revision>19</sites:revision></entry><entry gd:etag="&quot;USl7JmA9&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/7902698018897508532</id><published>2011-11-23T14:51:38.407Z</published><updated>2011-11-24T11:31:31.915Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-24T11:31:31.913Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Java 7 Updates</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><div class="sites-layout-name-one-column-hf sites-layout-vbox"><div class="sites-layout-tile sites-tile-name-header"><div dir="ltr"><div dir="ltr">It has been quite sometime that Java 7 got released with plenty of new features and enhancements that shall interest Java developer community. Following sections of this page cover some of these changes with examples. </div></div></div><div class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:400px;"><div class="sites-embed-content sites-embed-type-toc"><div class="goog-toc sites-embed-toc-maxdepth-1"><p>Contents</p><ol class="goog-toc"><li class="goog-toc"><a href="#TOC-Strings-in-switch-statements"><strong>1 </strong>Strings in switch statements</a></li><li class="goog-toc"><a href="#TOC-The-diamond-operator-"><strong>2 </strong>The diamond operator "&lt;&gt;"</a></li><li class="goog-toc"><a href="#TOC-Handling-more-than-one-type-of-exce"><strong>3 </strong>Handling more than one type of exception</a></li><li class="goog-toc"><a href="#TOC-Re-throwing-exceptions-with-more-in"><strong>4 </strong>Re-throwing exceptions with more inclusive type checking</a></li><li class="goog-toc"><a href="#TOC-The-try-with-resources-statement"><strong>5 </strong>The try-with-resources statement</a></li><li class="goog-toc"><a href="#TOC-Numeric-literals-with-underscores"><strong>6 </strong>Numeric literals with underscores</a></li><li class="goog-toc"><a href="#TOC-Binary-literals"><strong>7 </strong>Binary literals</a></li><li class="goog-toc"><a href="#TOC-Fork-and-Join"><strong>8 </strong>Fork and Join</a></li><li class="goog-toc"><a href="#TOC-Supporting-dynamism"><strong>9 </strong>Supporting dynamism</a></li></ol></div></div></div></div></div><br /></div><div dir="ltr"><h3><a name="TOC-Strings-in-switch-statements" />Strings in switch statements</h3><div><div>In the JDK 7 release, you can use a String object in the expression of a switch statement:</div><div><br /></div><div><code>public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {</code></div><div><code>     String typeOfDay;</code></div><div><code>     switch (dayOfWeekArg) {</code></div><div><code>         case "Monday":</code></div><div><code>             typeOfDay = "Start of work week";</code></div><div><code>             break;</code></div><div><code>         case "Tuesday":</code></div><div><code>         case "Wednesday":</code></div><div><code>         case "Thursday":</code></div><div><code>             typeOfDay = "Midweek";</code></div><div><code>             break;</code></div><div><code>         case "Friday":</code></div><div><code>             typeOfDay = "End of work week";</code></div><div><code>             break;</code></div><div><code>         case "Saturday":</code></div><div><code>         case "Sunday":</code></div><div><code>             typeOfDay = "Weekend";</code></div><div><code>             break;</code></div><div><code>         default:</code></div><div><code>             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);</code></div><div><code>     }</code></div><div><code>     return typeOfDay;</code></div><div><code>}</code></div></div><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><h3><a name="TOC-The-diamond-operator-" />The diamond operator "&lt;&gt;"</h3><div><div>You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (&lt;&gt;) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond.</div><div><br /></div><div>For example, consider the following variable declaration:</div><div><br /></div><div><code>Map&lt;String, List&lt;String&gt;&gt; myMap = new HashMap&lt;String, List&lt;String&gt;&gt;();</code></div><div><br /></div><div>In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (&lt;&gt;):</div><div><br /></div><div><code>Map&lt;String, List&lt;String&gt;&gt; myMap = new HashMap&lt;&gt;();</code></div><div><br /></div><div>Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile:</div><div><br /></div><div><code>List&lt;String&gt; list = new ArrayList&lt;&gt;();</code></div><div><code>list.add("A");</code></div><div><span style="color:rgb(0,96,0);font-family:monospace">list.addAll(new ArrayList&lt;&gt;()); </span><code>// Statement should fail since addAll expects</code><span style="color:rgb(0,96,0);font-family:monospace"> Collection&lt;? extends String&gt;</span></div><div><br /></div><div>In comparison, the following example compiles:</div><div><br /></div><div><code>List&lt;? extends String&gt; list2 = new ArrayList&lt;&gt;();</code></div><div><code>list.addAll(list2);</code></div></div><div><div><br /></div><div><b>Note:</b> The diamond often works in method calls; however, it is suggested that you use the diamond primarily for variable declarations.<br /></div></div><h3><a name="TOC-Handling-more-than-one-type-of-exce" />Handling more than one type of exception</h3><div><div>In Java SE 7 and later, a single catch block can handle more than one type of exception. Consider the following example, which contains duplicate code in each of the catch blocks:</div><div><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><br /></div><div><code>//Prior to Java 7</code></div><div><code>catch (IOException ex) {</code></div><div><code>     logger.log(ex);</code></div><div><code>     throw ex;</code></div><div><code>catch (SQLException ex) {</code></div><div><code>     logger.log(ex);</code></div><div><code>     throw ex;</code></div><div><code>}</code></div><div><br /></div><div>In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.</div><div><br /></div><div>The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:</div><div><br /></div><div><code>catch (IOException|SQLException ex) { // java 7 handling more than one type of exception</code></div><div><code>    logger.log(ex);</code></div><div><code>    throw ex;</code></div><div><code>}</code></div><div><br /></div><div>The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|). Some other advantages apart from syntactical improvement:</div><div><div><ul><li>Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. </li><li>A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers. </li></ul></div></div><div><br /></div><div><b>Note:</b> If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.</div><h3><a name="TOC-Re-throwing-exceptions-with-more-in" />Re-throwing exceptions with more inclusive type checking</h3></div><div><span style="font-size:small">The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in the throws clause of a method declaration.</span></div><div><div><font size="2"><br /></font></div><div><font size="2">Consider the following example:</font></div><div><font size="2"><br /></font></div><div><font size="2"><code>  static class FirstException extends Exception { }</code></font></div><div><font size="2"><code>  static class SecondException extends Exception { }</code></font></div><div><font size="2"><br /></font></div><div><font size="2"><code>  public void rethrowException(String exceptionName) throws Exception {</code></font></div><div><font size="2"><code>    try {</code></font></div><div><font size="2"><code>      if (exceptionName.equals("First")) {</code></font></div><div><font size="2"><code>        throw new FirstException();</code></font></div><div><font size="2"><code>      } else {</code></font></div><div><font size="2"><code>        throw new SecondException();</code></font></div><div><font size="2"><code>      }</code></font></div><div><font size="2"><code>    } catch (Exception e) {</code></font></div><div><font size="2"><code>      throw e;</code></font></div><div><font size="2"><code>    }</code></font></div><div><font size="2"><code>  }</code></font></div><div><font size="2"><br /></font></div><div><font size="2">This examples's try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.</font></div><div><font size="2"><br /></font></div><div><font size="2">However, in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:</font></div><div><font size="2"><br /></font></div><div><font size="2">  <code>public void rethrowException(String exceptionName)</code></font><span style="color:rgb(0,96,0);font-family:monospace;font-size:small"> throws FirstException, SecondException {</span></div><div><font size="2"><code>    try {</code></font></div><div><font size="2"><code>      // ...</code></font></div><div><font size="2"><code>    }</code></font></div><div><font size="2"><code>    catch (Exception e) {</code></font></div><div><font size="2"><code>      throw e;</code></font></div><div><font size="2"><code>    }</code></font></div><div><font size="2"><code>  }</code></font></div><div><font size="2"><br /></font></div><div><font size="2">This analysis is disabled if the catch parameter is assigned to another value in the catch block. However, if the catch parameter is assigned to another value, you must specify the exception type Exception in the throws clause of the method declaration.</font></div><div><font size="2"><br /></font></div><div><font size="2">In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:</font></div><div><ul><li><span style="font-size:small">The try block is able to throw it.</span></li><li><span style="font-size:small">There are no other preceding catch blocks that can handle it.</span></li><li><span style="font-size:small">It is a subtype or supertype of one of the catch clause's exception parameters.</span></li></ul></div><div><span style="font-size:small">The Java SE 7 compiler allows you to specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration because you can rethrow an exception that is a supertype of any of the types declared in the throws.</span></div><div><font size="2"><br /></font></div><div><font size="2">In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of the catch clause's exception parameters. A compiler from a release prior to Java SE 7 generates the error, "unreported exception Exception; must be caught or declared to be thrown" at the statement throw e. The compiler checks if the type of the exception thrown is assignable to any of the types declared in the throws clause of the rethrowException method declaration. However, the type of the catch parameter e is Exception, which is a supertype, not a subtype, of FirstException andSecondException.</font></div></div><h3><a name="TOC-The-try-with-resources-statement" />The try-with-resources statement</h3><div><div>The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements <b>java.lang.AutoCloseable</b>, which includes all objects which implement <b>java.io.Closeable</b>, can be used as a resource.</div><div><br /></div><div>The following example reads the first line from a file. It uses an instance of BufferedReader to read data from the file. BufferedReader is a resource that must be closed after the program is finished with it:</div><div><br /></div><div><span style="color:rgb(0,96,0);font-family:monospace">// Java 7 Code</span></div><div><code>static String readFirstLineFromFile(String path) throws IOException { </code></div><div><code>  try (BufferedReader br = new BufferedReader(new FileReader(path))) {</code></div><div><code>    return br.readLine();</code></div><div><code>  }<b>//no finally block required as resources will be closed automatically</b></code></div><div><code>}</code></div><div><br /></div><div>In this example, the resource declared in the try-with-resources statement is a BufferedReader. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).</div><div><br /></div><div>Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the whether the try statement completes normally or abruptly. The following example uses a finally block instead of a try-with-resources statement:</div><div><br /></div><div><span style="color:rgb(0,96,0);font-family:monospace">//Before Java 7</span></div><div><code>static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {</code></div><div><code>  BufferedReader br = new BufferedReader(new FileReader(path));</code></div><div><code>  try {</code></div><div><code>    return br.readLine();</code></div><div><code>  } finally { </code></div><div><code>    if (br != null) br.close();</code></div><div><code>  }</code></div><div><code>}</code></div><div><br /></div><div>You may declare one or more resources in a try-with-resources statement.</div><div><br /></div><div><b>Note:</b> A try-with-resources statement can still have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.</div></div><h3><a name="TOC-Numeric-literals-with-underscores" />Numeric literals with underscores</h3><div><div>In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.</div><div><br /></div><div>You can place underscores only between digits; you cannot place underscores in the following places:</div><div><ul><li>At the beginning or end of a number</li><li>Adjacent to a decimal point in a floating point literal</li><li>Prior to an F or L suffix</li><li>In positions where a string of digits is expected</li></ul></div><div>The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:</div><div><br /></div><div><code>float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point</code></div><div><code>float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point</code></div><div><code>long ssn</code><code>= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix</code></div><div><br /></div><div><code>int x1 = _52;              // This is an identifier, not a numeric literal</code></div><div><code>int x2 = 5_2;              // OK (decimal literal)</code></div><div><code>int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal</code></div><div><code>int x4 = 5_______2;        // OK (decimal literal)</code></div><div><br /></div><div><code>int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix</code></div><div><code>int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number</code></div><div><code>int x7 = 0x5_2;            // OK (hexadecimal literal)</code></div><div><code>int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number</code></div><div><br /></div><div><code>int x9 = 0_52;             // OK (octal literal)</code></div><div><code>int x10 = 05_2;            // OK (octal literal)</code></div><div><code>int x11 = 052_;            // Invalid; cannot put underscores at the end of a number</code></div><div /><h3><a name="TOC-Binary-literals" />Binary literals<span style="font-size:13px;font-weight:normal;white-space:pre">		</span></h3><div>In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. The following examples show binary literals:</div><div><br /></div><div><code>// An 8-bit 'byte' value:</code></div><div><code>byte aByte = (byte)0b00100001;</code></div><div><br /></div><div><code>// A 16-bit 'short' value:</code></div><div><code>short aShort = (short)0b1010000101000101;</code></div><div><br /></div><div><code>// Some 32-bit 'int' values:</code></div><div><code>int anInt1 = 0b10100001010001011010000101000101;</code></div><div><code>int anInt2 = 0b101;</code></div><div><code>int anInt3 = 0B101; // The B can be upper or lower case.</code></div><div><br /></div><div><code>// A 64-bit 'long' value. Note the "L" suffix:</code></div><div><code>long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;</code></div></div><h3><a name="TOC-Fork-and-Join" />Fork and Join</h3><div><div>The effective use of parallel cores in a Java program has always been a challenge. There were few home-grown frameworks that would distribute the work across multiple cores and then join them to return the result set. Java 7 has incorporated this feature as a Fork and Join framework.</div><div><br /></div><div>Basically the Fork-Join breaks the task at hand into mini-tasks until the mini-task is simple enough that it can be solved without further breakups. It's like a divide-and-conquer algorithm. One important concept to note in this framework is that ideally no worker thread is idle. They implement a work-stealing algorithm in that idle workers "steal" the work from those workers who are busy.</div><div><br /></div><div>The core classes supporting the Fork-Join mechanism are ForkJoinPool and ForkJoinTask. The ForkJoinPool is basically a specialized implementation of ExecutorService implementing the work-stealing algorithm.</div></div><div><h3><a name="TOC-Supporting-dynamism" />Supporting dynamism</h3><div>Java is a statically typed language — the type checking of the variables, methods and return values is performed at compile time. The JVM executes this strongly-typed bytecode at runtime without having to worry about finding the type information.</div><div><br /></div><div>There's another breed of typed languages — the dynamically typed languages. Ruby, Python and Clojure are in this category. The type information is unresolved until runtime in these languages. This is not possible in Java as it would not have any necessary type information.</div><div><br /></div><div>There is an increasing pressure on Java folks improvise running the dynamic languages efficiently. Although it is possible to run these languages on a JVM (using Reflection), it's not without constraints and restrictions.</div><div><br /></div><div>In Java 7, a new feature called invokedynamic was introduced. This makes VM changes to incorporate non-Java language requirements. A new package, java.lang.invoke, consisting of classes such as MethodHandle, CallSite and others, has been created to extend the support of dynamic languages.</div></div></div></div></div><div class="sites-layout-tile sites-tile-name-footer sites-layout-empty-tile"><div dir="ltr"><br /></div></div></div></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/Java7Updates" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/7902698018897508532" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7902698018897508532" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7902698018897508532" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>Java7Updates</sites:pageName><sites:revision>18</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/8457386111807034219</id><published>2010-12-29T18:17:11.282Z</published><updated>2011-11-18T17:57:12.845Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-18T17:57:12.844Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Use final liberally</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr"><div>Use the final keyword liberally to communicate your intent. The final keyword has more than one meaning :</div><div><ul><li>a final class cannot be extended</li><li>a final method cannot be overridden</li><li>final fields, parameters, and local variables cannot change their value once set</li></ul></div><div>In the last case, "value" for primitives is understood in the usual sense, while "value" for objects means the object's identity, not its state. Once the identity of a final object reference is set, it can still change its state, but not its identity. Declaring primitive fields as final automatically ensures thread-safety for that field.</div><div><br /></div><div>Some habitually declare parameters as final, since this almost always is the desired behaviour. Others find this verbose, and of little real benefit.</div><div><br /></div><div>Consistently using final with local variables (when appropriate) can be useful as well. It brings attention to the non-final local variables, which usually have more logic associated with them (for example, result variables, accumulators, loop variables). Many find this verbose. A reasonable approach is to use final for local variables only if there is at least one non-final local variable in the method ; this serves to quickly distinguish the non-final local variables from the others.</div><div><br /></div><div>Using final :</div><div><ul><li>clearly communicates your intent</li><li>allows the compiler and virtual machine to perform minor optimizations</li><li>clearly flags items which are simpler in behavior - final says,  "If you are looking for complexity, you won't find it here."</li></ul></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/usefinalliberally" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/8457386111807034219" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8457386111807034219" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8457386111807034219" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>usefinalliberally</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YDkpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/5188286422978133947</id><published>2010-12-29T13:49:53.066Z</published><updated>2011-11-17T17:10:39.302Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-17T17:10:39.300Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Avoiding null pointer exceptions</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr">Null pointer exceptions(NPE) are the undoubtedly the most common and most annoying errors. In most cases I have observed that it could have been avoided by simply sticking to some best coding practices while writing code. Here is an example of a potential NPE.<div><span><br /></span></div><div><span><code>  </code><span><code>  // BAD CODE</code></span></span><br /></div><div><div><font color="#006000" face="monospace"><div>    private Boolean isExpired(final StatusEnum status) {</div><div>        if (status.equals(StatusEnum.EXPIRED)) { // Potential null pointer if status is null.</div><div>            return Boolean.TRUE;</div><div>        } else {</div><div>            return Boolean.FALSE;</div><div>        }</div><div>    }</div></font></div><div><br /></div></div><div>Here if the "status" that is passed to the method is null you will get a NPE at the first statement in the method. However if we write the code like as follows it can be avoided.</div><div><br /></div><div><div style="color:rgb(0,96,0);font-family:monospace">    //GOOD CODE</div><div style="color:rgb(0,96,0);font-family:monospace"><span>    </span>private Boolean isExpired(final StatusEnum status) {</div><div><font color="#006000" face="monospace">        if (StatusEnum.EXPIRED.equals(status)) { // Move variable part as</font><span style="color:rgb(0,96,0);font-family:monospace"> parameter to equals method.</span></div><div style="color:rgb(0,96,0);font-family:monospace">            return Boolean.TRUE;</div><div style="color:rgb(0,96,0);font-family:monospace">        } else {</div><div style="color:rgb(0,96,0);font-family:monospace">            return Boolean.FALSE;</div><div style="color:rgb(0,96,0);font-family:monospace">        }</div><div style="color:rgb(0,96,0);font-family:monospace">    }</div></div><p>Some editors like IntelliJ provide a quick fix for similar use cases. If you have object.equals(”string literal”) it can replace with “string literal”.equals(object)  and you can do the replace all on your entire code base in one go if you wish. </p><p>Happy coding!!</p></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/avoidingnullpointerexceptions" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/5188286422978133947" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/5188286422978133947" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/5188286422978133947" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>avoidingnullpointerexceptions</sites:pageName><sites:revision>5</sites:revision></entry><entry gd:etag="&quot;WCl7JmA9&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/6724146667014838254</id><published>2011-08-04T06:18:14.874Z</published><updated>2011-11-17T12:49:31.605Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-17T12:49:31.604Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Awesome place to access Java source code online</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div dir="ltr">

<div dir="ltr">I thought it would be useful to have quick access to
entire java source code and for those open source projects that you don't have the source for while developing our java based applications. I came across
this awesome link while searching for java source code online and now I visit it almost everyday. You can browse fully cross-references java source code from Maven repository just like you do in your IDE. I found it very useful and recommend it to every Java developer.</div><p><a href="http://www.mavenjava.com/" target="_blank">http://www.mavenjava.com/</a></p></div></div><div dir="ltr"><div class="sites-embed-align-left-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/awesomeplacetoaccessjavasourcecodeonline" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/6724146667014838254" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6724146667014838254" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6724146667014838254" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>awesomeplacetoaccessjavasourcecodeonline</sites:pageName><sites:revision>11</sites:revision></entry><entry gd:etag="&quot;YDkpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/8948345882473341488</id><published>2010-12-31T11:00:25.067Z</published><updated>2011-11-15T07:35:12.275Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-31T11:43:21.327Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Crave for Immutable classes</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:336px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=336;
google_ad_height=280;
google_ad_format="336x280_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div><div dir="ltr">In the book "Effective Java", Joshua Bloch makes this compelling recommendation :</div><div dir="ltr"><div><div><b><font size="3">"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible."</font></b></div></div><div><br /></div><div>Immutable objects are objects whose data or properties cannot be changed after it is constructed. JDK has a number of immutable class like <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html" target="_blank">String</a> and <a href="http://java.sun.com/javase/6/docs/api/java/lang/Integer.html" target="_blank">Integer</a>. Immutable objects have big list of compelling positive qualities and they can greatly simplify your program. Immutable objects :</div><div><ul><li>are simple to construct, test, and use</li><li>are automatically thread-safe and have no synchronization issues</li><li>do not need a copy constructor</li><li>do not need an implementation of clone</li><li>allow hashCode to use lazy initialization, and to cache its return value</li><li>do not need to be copied defensively when used as a field</li><li>make good Map keys and set elements (these objects must not change state while in the collection)</li><li>have their class invariant established once upon construction, and it never needs to be checked again</li><li>always have "failure atomicity" (a term used by Joshua Bloch in his book "Effective Java") : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state</li></ul></div><div>They are among the simplest and most robust kinds of classes you can possibly build in JAVA. When you create immutable classes, entire categories of problems simply disappear.</div><div><br /></div><div>Make a class immutable by following these guidelines :</div><div><ul><li>ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private</li><li>make fields private and final</li><li>force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods (that is, avoid the Java Beans convention)</li><li>do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state</li><li>if the class has any mutable object fields, then they must be defensively copied when passed between the class and its caller</li></ul><div><b><font size="3">Here is an example</font></b>. Please go through the comments that are there in the code to understand the concept.</div></div><div><hr /></div><div><div><code>package com.javagyan.examples;</code></div><div><span style="color:rgb(0,96,0);font-family:monospace"><br /></span></div><div><span style="color:rgb(0,96,0);font-family:monospace">import java.util.Date;</span></div><div><br /></div><div><code>/**</code></div><div><code>* Planet is an immutable class, since there is no way to change </code><span style="color:rgb(0,96,0);font-family:monospace">its state after construction.</span></div><div><code>*/</code></div><div><code>public final class Planet {</code></div><div><br /></div><div><code>  public Planet (double aMass, String aName, Date aDateOfDiscovery) {</code></div><div><code>     fMass = aMass;</code></div><div><code>     fName = aName;</code></div><div><code>   <b>  //make a private copy of aDateOfDiscovery</b></code></div><div><code><b>     //this is the only way to keep the fDateOfDiscovery</b></code></div><div><code><b>     //field private, and shields this class from any changes that </b></code></div><div><code><b>     //the caller may make to the original aDateOfDiscovery object</b></code></div><div><code>     fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());</code></div><div><code>  }</code></div><div><br /></div><div><code>  /**</code></div><div><code>  * <b>Returns a primitive value.</b></code></div><div><code>  *</code></div><div><code>  * <b>The caller can do whatever they want with the return value, without </b></code></div><div><code><b>  * affecting the internals of this class. Why? Because this is a primitive </b></code></div><div><code><b>  * value. The caller sees its "own" double that simply has the</b></code></div><div><code><b>  * same value as fMass.</b></code></div><div><code>  * @return double</code></div><div><code>  */</code></div><div><code>  public double getMass() {</code></div><div><code>    return fMass;</code></div><div><code>  }</code></div><div><br /></div><div><code>  /**</code></div><div><code>  * <b>Returns an immutable object.</b></code></div><div><code>  *</code></div><div><code>  * The caller gets a direct reference to the internal field. But this is not </code></div><div><code>  * dangerous, since String is immutable and cannot be changed.</code></div><div><code> * @return String</code></div><div><code>  */</code></div><div><code>  public String getName() {</code></div><div><code>    return fName;</code></div><div><code>  }</code></div><div><br /></div><div><code>//  /**</code></div><div><code>//  * <b>Returns a mutable object - likely bad style.</b></code></div><div><code>//  *</code></div><div><code>//  *<b> The caller gets a direct reference to the internal field. This is usually dangerous, </b></code></div><div><code><b>//  * since the Date object state can be changed both by this class and its caller.</b></code></div><div><code><b>//  * That is, this class is no longer in complete control of fDate.</b></code></div><div><code>//  */</code></div><div><code>//  public Date getDateOfDiscovery() {</code></div><div><code>//    return fDateOfDiscovery;</code></div><div><code>//  }</code></div><div><br /></div><div><code>  /**</code></div><div><code>  *<b> Returns a mutable object - good style.</b></code></div><div><code>  * </code></div><div><code>  * Returns a defensive copy of the field.</code></div><div><code>  * The caller of this method can do anything they want with the</code></div><div><code>  * returned Date object, without affecting the internals of this</code></div><div><code>  * class in any way. Why? Because they do not have a reference to </code></div><div><code>  * fDate. Rather, they are playing with a second Date that initially has the </code></div><div><code>  * same data as fDate.</code></div><div><code> * @return Date</code></div><div><code>  */</code></div><div><code>  public Date getDateOfDiscovery() {</code></div><div><code>    return new Date(fDateOfDiscovery.getTime());</code></div><div><code>  }</code></div><div><br /></div><div><code>  // PRIVATE //</code></div><div><br /></div><div><code>  /**</code></div><div><code>  * Final primitive data is always immutable.</code></div><div><code>  */</code></div><div><code>  private final double fMass;</code></div><div><br /></div><div><code>  /**</code></div><div><code>  * An immutable object field. (String objects never change state.)</code></div><div><code>  */</code></div><div><code>  private final String fName;</code></div><div><br /></div><div><code>  /**</code></div><div><code>  * A mutable object field. In this case, the state of this mutable field</code></div><div><code>  * is to be changed only by this class. (In other cases, it makes perfect</code></div><div><code>  * sense to allow the state of a field to be changed outside the native</code></div><div><code>  * class; this is the case when a field acts as a "pointer" to an object</code></div><div><code>  * created elsewhere.)</code></div><div><code>  */</code></div><div><code>  private final Date fDateOfDiscovery;</code></div><div><code>}</code></div></div></div><div dir="ltr"><br /></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/creatingimmutableclasses" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/8948345882473341488" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8948345882473341488" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/8948345882473341488" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>creatingimmutableclasses</sites:pageName><sites:revision>5</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/2823742902810115443</id><published>2010-12-29T11:46:09.036Z</published><updated>2011-11-11T14:17:47.517Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:17:47.515Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Change Default Author Name for JavaDocs in Eclipse</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr">The auto generated Java docs at the class level picks the user name from the system user. This could result in weird author names in your code files as in an organization usernames are usually as per organizational naming conventions. For example my user name that I use to login is something like SK0012345 and you will agree that it wouldn't look good as an author name in a Java file and might not make any sense to most other viewers of the code. <div><div><code><br /></code></div><div><code>/**</code></div><div><code> * Test default author in JavaDocs</code></div><div><code> * @author SK0012345</code></div><div><code> */</code></div><div><code>public class TestClass {</code></div><div><span style="color:rgb(0,96,0);font-family:monospace">}</span></div></div><div><code><br /></code></div><div><span style="color:rgb(40,55,105);font-family:Arial,sans-serif">Here is a quick way to change the default author name in your Eclipse projects. Simply edit your eclipse.ini file found in the root directory where you placed Eclipse. I have Eclipse at C:\devtools\development\eclipse, so my path would be C:\devtools\development\eclipse\eclipse.ini. Once editing this file add the following line and save.</span></div><div><div><code><br /></code></div><div><code>-Duser.name=Sanjeev Kumar</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><br /></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif">After saving restart Eclipse and when you do a JavaDoc comment and use the author attribute by typing @author and pressing enter on the autocomplete you will see something like this:</div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><br /></div><div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)">/**</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)"> * Test default author in JavaDocs</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)"> * @author Sanjeev Kumar</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)"> */</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)">public class TestClass {</code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><span style="color:rgb(0,96,0);font-family:monospace">}</span></div></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif"><code style="color:rgb(0,96,0)"><br /></code></div><div style="color:rgb(40,55,105);font-family:Arial,sans-serif">Simple yet useful. Happy coding!</div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/changedefaultauthornameforjavadocsineclipse" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/2823742902810115443" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2823742902810115443" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2823742902810115443" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>changedefaultauthornameforjavadocsineclipse</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YDkpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/7628105631636204859</id><published>2010-12-29T12:42:32.957Z</published><updated>2011-11-11T14:15:14.348Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:15:14.346Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Eclipse Keyboard Shortcuts</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr"><div>These keybindings are made by Carsten Ullrich (cullrich located-at activemath.org) and are released under the Creative Commons Attribution-ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).</div><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-on sites-embed sites-embed-full-width" style="width:100%;"><h4 class="sites-embed-title">Eclipse Keyboard Shortcuts</h4><div class="sites-embed-object-title" style="display:none;">Google Spreadsheet</div><div class="sites-embed-content sites-embed-type-spreadsheet"><iframe src="http://spreadsheets.google.com/spreadsheet/pub?chrome=false&amp;key=0Ag_VihEyeaIudF9sREdTVi1XWmxGN0RFcXNCTFJSS2c&amp;output=html&amp;pubredirect=true&amp;widget=true" width="100%" height="600" title="Google Spreadsheet" frameborder="0" id="279925800" /></div></div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/EclipseKeyboardShortcuts" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/7628105631636204859" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7628105631636204859" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7628105631636204859" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>EclipseKeyboardShortcuts</sites:pageName><sites:revision>5</sites:revision></entry><entry gd:etag="&quot;YD4peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/600030015211406623</id><published>2010-12-29T12:58:13.703Z</published><updated>2011-11-11T14:14:13.032Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:14:13.031Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Prefer using parameterized types over raw types</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div><br /></div><div dir="ltr"><div>When generics were introduced in JDK 1.5, raw types were retained only to maintain backwards compatibility with older versions of Java. Although using raw types is still possible, they should be avoided for following reasons :</div><div><ul><li>they usually require casts</li><li>they aren't type safe, and some important kinds of errors will only appear at runtime</li><li>they are less expressive, and don't self-document in the same way as parameterized types</li></ul><div>Unless you are using a JDK version prior to 1.5 I don't see a reason why you should not use parameterized types in your code.</div></div><div><br /></div><div>Happy coding !!</div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/preferusingparameterizedtypesoverrawtypes" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/600030015211406623" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/600030015211406623" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/600030015211406623" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>preferusingparameterizedtypesoverrawtypes</sites:pageName><sites:revision>2</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/6668353301651222412</id><published>2010-12-29T14:20:01.785Z</published><updated>2011-11-11T14:11:51.336Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:11:51.334Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Prefer switch over if-else</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr"><div>In most cases switch will be lighter and performs faster than an if/else ladder. The compiler is able to optimize switch statements into a lookup table and perform compile-time checking for literals when dealing with enumerations, so I'd suggest that it's usually preferable to use switch over if/else if if you're dealing with numeric or enum types in Java. </div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/preferswitchoverif-else" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/6668353301651222412" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6668353301651222412" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6668353301651222412" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>preferswitchoverif-else</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/5436513406259681139</id><published>2010-12-29T16:49:58.624Z</published><updated>2011-11-11T14:10:36.901Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:10:36.899Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Converting Arrays to String</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr">Sometimes it may be required to display an array in string format. Expected result should be that all the elements of the Arrays gets printed out the way it is done for any collection. However the default toString() method is not very informative and does not include any array content. I have seen developers doing weird stuff to achieve this which includes writing helper methods and quite a few lines of code. <div><br /></div><div>However there are easier ways available with in Java API to achieve the same output. To provide more useful representations of arrays, various toString methods (and the deepToString method) were added to the Arrays class in JDK 1.5. Those methods can be used when available, as in :</div><div><br /></div><div><code>Arrays.</code><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#toString(java.lang.Object%5B%5D)" target="_blank"><code>toString</code></a><code>(myArray); </code></div><div><code>Arrays.</code><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#deepToString(java.lang.Object%5B%5D)" target="_blank"><code>deepToString</code></a><code>(myObjectArray); //recursive</code></div><div><br /></div><div>If you need an alternative, you can simply convert the array to a collection, as in</div><div><br /></div><div><code>Arrays.</code><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)" target="_blank"><code>asList</code></a><code>(myArray).toString();</code></div><p>Happy coding !!!</p></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/convertingarraystostring" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/5436513406259681139" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/5436513406259681139" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/5436513406259681139" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>convertingarraystostring</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/6026573280042233840</id><published>2010-12-29T17:26:53.274Z</published><updated>2011-11-11T14:09:44.986Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:09:44.984Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Prefer composition over inheritance</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr">Trust me on this one, I have learnt it the hard way. To start with lets start with the understanding of the terms composition and inheritance<div><div><ul><li>Composition</li><ul><li>Functionality of an object is made up of an aggregate of different classes.  </li><li>In practice, this means holding a pointer to another class to which work is deferred.</li><li>Collaboration is implemented simply by forwarding all calls to an object field</li><li>it has no dependence on implementation details of the object field</li><li>it is more flexible, since it is defined dynamically at run-time, not statically at compile-time</li></ul><li>Inheritance</li><ul><li>Functionality of an object is made up of it's own functionality plus functionality from its parent classes.</li></ul></ul><div><div>Sub-classing or inheritance has the following issues :</div><div><ul><li>it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled</li><li>new methods added to the superclass can break the subclass</li><li>superclass and subclass need to evolve in parallel</li><li>designing a class so that it may be safely extended takes extra work -  extending a class not explicitly designed for such use is risky</li><li>different packages are often under the control of different programmers - extending a class in a different package is risky</li></ul><div><div>A common exception is the template method design pattern. There, the safest style is to make all items in the abstract base class final, except for the single abstract method which needs to be implemented by the subclass.</div></div></div></div></div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/prefercompositionoverinheritance" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/6026573280042233840" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6026573280042233840" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/6026573280042233840" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>prefercompositionoverinheritance</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YD4peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/4981141638231304693</id><published>2010-12-29T18:06:01.339Z</published><updated>2011-11-11T14:08:00.491Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:08:00.488Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Overridable methods need special care</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=60;
google_ad_format="468x60_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr"><div><b>Allowing a method to be overridden should always be done intentionally, not by accident. </b>Any method which is not private, static, or final can be overridden. Over-ridable methods, and any methods which call them, represent unusual places in your code, to which special attention must be paid. This is because sub-classing violates encapsulation, in the sense that it is possible for a subclass to break its superclass's contract. If you do not intend a method to be overridden, then you should declare it as private, static, or final.</div><div><br /></div><div>When you do override a method, you should use the @Override annotation. This allows the compiler to verify that the method is indeed a valid override of an existing method. For example, your implementations of toString, equals, and hashCode should always use @Override in the method header. </div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/overridablemethodsneedspecialcare" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/4981141638231304693" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4981141638231304693" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4981141638231304693" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>overridablemethodsneedspecialcare</sites:pageName><sites:revision>2</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/4375446729169789859</id><published>2010-12-29T18:23:16.014Z</published><updated>2011-11-11T14:05:37.052Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:05:37.051Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Beware of floating point numbers</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div><div dir="ltr"><div>Outside of a scientific or engineering context, the use of float and double (and the corresponding wrapper classes Float and Double ) should likely be avoided. The fundamental problem is that rounding errors will always occur when using these data types - they are unavoidable.</div><div><br /></div><div>In a typical business application, using float and double to represent money values is dangerous, because of these rounding issues. Instead, BigDecimal should usually be used to represent money. It is also a common practice to have utility classes like "MonetaryAmount.java" that are wrappers over "BigDecimal" class of the JDK and that provides helper methods and functionality as needed by the business application.</div><div><br /></div><div>From an IBM article on this topic :</div><div><br /></div><div><i>"...binary floating-point arithmetic should not be used for financial, commercial, and user-centric applications or web services because the decimal data used in these applications cannot be represented exactly using binary floating-point."</i></div><div><br /></div><div>From an article by Brian Goetz :</div><div><br /></div><div><i>"...it is a bad idea to use floating point to try to represent exact quantities like monetary amounts. Using floating point for dollars-and-cents calculations is a recipe for disaster. Floating point numbers are best reserved for values such as measurements, whose values are fundamentally inexact to begin with." </i></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/bewareoffloatingpointnumbers" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/4375446729169789859" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4375446729169789859" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4375446729169789859" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>bewareoffloatingpointnumbers</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YDopeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/3143680761285091236</id><published>2010-12-30T08:17:02.531Z</published><updated>2011-11-11T14:03:05.829Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:03:05.827Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Quick tips to improve code quality in agile teams</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div></div></div></div><div dir="ltr"><div dir="ltr"><div>Here are a few quick pointers to improve code quality in agile teams and to deliver stories completely without leaving bugs behind.<br /><ul><li style="list-style-position:outside;list-style-type:square">Improve how retrospectives are done so they can effectively detect and address these kinds of quality problems.</li><li style="list-style-position:outside;list-style-type:square">If there are quality differences in the code written by different team members, have the better programmers find ways to raise the quality of code written by others.</li><li style="list-style-position:outside;list-style-type:square">Focus on defect prevention, not just detection. Tools that can help are:</li><ul><li style="list-style-position:outside;list-style-type:square">code inspections</li><li style="list-style-position:outside;list-style-type:square">checklists</li></ul><li style="list-style-position:outside;list-style-type:square">Use pair-programming to improve knowledge sharing within the team. It seems expensive but mostly it is worth it.</li><li style="list-style-position:outside;list-style-type:square">Begin regular use of static or dynamic code analysis tools.</li></ul></div></div></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/quicktipstoimprovecodequalityinagileteams" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/3143680761285091236" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/3143680761285091236" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/3143680761285091236" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>quicktipstoimprovecodequalityinagileteams</sites:pageName><sites:revision>6</sites:revision></entry><entry gd:etag="&quot;YD8peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/2703056854264476058</id><published>2011-01-18T14:25:32.641Z</published><updated>2011-11-11T14:01:53.221Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:01:53.220Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Remote debugging on Jboss</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div class="sites-embed-align-center-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div><div dir="ltr">To debug your application on JBOSS server you would need to enable the debugging on your JBOSS application server. By default it is turned off. In order to set jboss app server to be running in debugging mode, you should uncomment following line in "jboss-5.1.0.GA/jboss/bin/run.conf"</div><div dir="ltr"><br />like this:<br /><br /># Sample JPDA settings for remote socket debugging<br />JAVA_OPTS=”$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=8787,serve <br /><br />Once done you can configure your eclipse remote application debugger on port 8787 and start debugging your application.<br /><br />Happy debugging !!</div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/remotedebuggingonjboss" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/2703056854264476058" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2703056854264476058" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2703056854264476058" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>remotedebuggingonjboss</sites:pageName><sites:revision>3</sites:revision></entry><entry gd:etag="&quot;YD4peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/2227204136420707216</id><published>2011-01-19T13:13:17.267Z</published><updated>2011-11-11T14:00:22.339Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T14:00:22.338Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Using Eclipse Effectively</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-off sites-embed" style="width:468px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=468;
google_ad_height=15;
google_ad_format="468x15_0ads_al_s";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div><br /></div><div dir="ltr">To ensure that the code that you write is always clean and complaint to 
your project specific coding standards and guidelines, it is important 
that you configure your eclipse to effectively use its compiler settings, Formatter, CheckStyle
 and related built in features. Most developers wouldn't bother to do so
 but trust me that it is huge time saver in long run and shall always keep your code 
quality under check.<br /><br />Following spread sheet has the configuration
 that we use in our current projects. You can customize these settings 
according to your development standards and needs. <br /><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-on sites-embed sites-embed-full-width" style="width:100%;"><h4 class="sites-embed-title">Eclipse Compiler Settings</h4><div class="sites-embed-object-title" style="display:none;">Google Spreadsheet</div><div class="sites-embed-content sites-embed-type-spreadsheet"><iframe src="http://spreadsheets.google.com/spreadsheet/loadredirect?chrome=false&amp;key=0Ap6Wf8mnIbkvdFZnZTNweGxOd2haZnpyN1NPSXNZNkE&amp;output=html&amp;pubredirect=true&amp;widget=true" width="100%" height="600" title="Google Spreadsheet" frameborder="0" id="1705885354" /></div></div></div><br />Also ideally your 
Eclipse should be configured with your "code formatter" and "CheckStyle" 
XML configurations. To start with you can simply import these xmls 
that are attached to this page to enable code formatting and checkstyle for your 
code. You can get more detail on CheckStyle at 
http://checkstyle.sourceforge.net/. <br /><br />Links to configure the CheckStyle, Formatter and compiler setting can be found under your Eclipse--&gt;Preferences menu.</div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/usingeclipseeffectively" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/2227204136420707216" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2227204136420707216" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/2227204136420707216" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>usingeclipseeffectively</sites:pageName><sites:revision>2</sites:revision></entry><entry gd:etag="&quot;YDkpeyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/4259084888760043399</id><published>2011-08-18T09:47:11.463Z</published><updated>2011-11-11T13:59:21.351Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-11T13:59:21.348Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Skip over certain classes when using Step Into(F5) in Eclipse’s debugger</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div dir="ltr"><span style="font-family:trebuchet ms,sans-serif">Whenever I use the Step Into feature (F5) in Eclipse’s debugger, I’m 
mainly interested in stepping through code in my  own classes, not the 
ones from external libraries or even Java classes.</span></div><div dir="ltr">
<p style="font-family:trebuchet ms,sans-serif">For example, there’s almost no reason to ever want to step into 
Spring’s code or proxy classes (other than to learn more about them or 
maybe debug a potential bug in Spring). And normally I’m not interested 
in Java util classes (eg. ArrayList). This also goes for Hibernate, 
Apache Commons, Google and many other external libraries.</p>
<p style="font-family:trebuchet ms,sans-serif">Fortunately, Eclipse makes it easy to specify which classes to skip 
by allowing step filters. This makes it easier to focus on your own code
 and also keeps your editor area clean since Eclipse won’t be opening 
classes in separate editors all the time.</p>

<h3 style="font-family:trebuchet ms,sans-serif"><a name="TOC-Enable-Step-Filters" />Enable Step Filters</h3>
<p style="font-family:trebuchet ms,sans-serif">To use step filters, the first step is to enable it. Eclipse comes 
with some default filters so let’s start by enabling them all. This will
 filter all Java classes (ie.<i> java.*</i>) and all Sun classes (ie. <i>sun.*</i>).</p>
<ol style="font-family:trebuchet ms,sans-serif"><li>Go to <b>Window &gt; Preferences &gt; Java &gt; Debug &gt; Step Filtering</b>.</li><li>Select the option <i>Use Step Filters</i>.</li><li>Click <b>Select All</b> to enable all the filters.</li><li>Leave the other options below the filter list as-is.</li></ol>
<p style="font-family:trebuchet ms,sans-serif">Here’s an example of what it should look like:</p>
<div style="display:block;text-align:left"><div style="display:block;text-align:left"><a href="http://i.imgur.com/jO19t.jpg" imageanchor="1"><img border="0" src="http://i.imgur.com/jO19t.jpg" /></a></div></div>
<p style="font-family:trebuchet ms,sans-serif"><b>NB: </b>Even with step filters enabled, you can still 
set breakpoints within any of these classes (if you have the source) and
 Eclipse will still stop at the breakpoint. Step filtering only affects 
the way that Step Into works.</p>
<p style="font-family:trebuchet ms,sans-serif">Also note that Eclipse will still step into your classes if they’re called from the ignored classes. For example, when you call <i>Collections.sort(List, Comparator)</i> and pass your own <i>Comparator</i>
 implementation, Eclipse will not step into the sort code, but it will 
step into your Comparator when it’s called by the sort code.</p>
<p style="font-family:trebuchet ms,sans-serif">If you want to change this behaviour (ie. prevent Eclipse from stopping in your method), then deselect <i>Step through filters.</i>
 However, I’d recommend only doing this if you’ve tried out the default,
 because most times you’ll probably want to step through your own code.</p>
<p style="font-family:trebuchet ms,sans-serif">The next step is to create some step filters of your own.</p>
<h3 style="font-family:trebuchet ms,sans-serif"><a name="TOC-Creating-your-own-step-filters" />Creating your own step filters</h3>
<p style="font-family:trebuchet ms,sans-serif">Once you’ve enabled step filters, all you have to do is add the 
classes you want to filter. Let’s assume that we want to ignore all 
classes from Spring, especially proxy classes.</p>
<ol style="font-family:trebuchet ms,sans-serif"><li>If you’re not there already, go to <b>Window &gt; Preferences &gt; Java &gt; Debug &gt; Step Filtering</b>.</li><li>Click <b>Add Filter…</b> A dialog should appear prompting you to enter a pattern.<b><br />
</b></li><li>Enter a regular expression for the classes you want to filter in the <i>Pattern to filter</i> field then click <b>Ok</b>. In our example, enter <i>org.springframework.* </i>(see image below). It’s easier to specify the top level package name with an asterix at the end.</li><li>Add another filter with the pattern <i>$Proxy*</i> to skip over Spring proxy classes (eg. when using Spring Transactions).</li><li>Click <b>Ok</b> on the Preferences dialog when you’re done.</li></ol>
<p style="font-family:trebuchet ms,sans-serif">Here’s what the step filter pattern dialog should look like:</p>
<div style="display:block;text-align:left"><a href="http://i.imgur.com/2RWRS.jpg" imageanchor="1"><img border="0" src="http://i.imgur.com/2RWRS.jpg" /></a></div>
<p style="font-family:trebuchet ms,sans-serif">Now when you use the debugger, you won’t be taken into Spring classes when you use Step Into (F5).</p>
<h3 style="font-family:trebuchet ms,sans-serif"><a name="TOC-Some-ideas-for-custom-filters" />Some ideas for custom filters</h3>
<p style="font-family:trebuchet ms,sans-serif">In addition to the Spring classes, you might also want to consider 
adding the following common libraries to your step filters to make 
debugging easier:</p>
<ul style="font-family:trebuchet ms,sans-serif"><li>org.apache.*</li><li>org.hibernate.*</li><li>com.google.*</li><li>org.eclipse.*</li><li>org.osgi.*</li></ul>
<p style="font-family:trebuchet ms,sans-serif">The last two are especially useful if you’re doing Eclipse RCP and/or OSGi development.</p><p style="font-family:trebuchet ms,sans-serif">Source : The article is republished from <a href="http://eclipseone.wordpress.com/">Eclipse On E</a></p></div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/skipovercertainclasseswhenusingstepintoineclipse%E2%80%99sdebugger" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/4259084888760043399" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4259084888760043399" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/4259084888760043399" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>skipovercertainclasseswhenusingstepintoineclipse’sdebugger</sites:pageName><sites:revision>5</sites:revision></entry><entry gd:etag="&quot;YD0peyY.&quot;"><id>http://sites.google.com/feeds/content/javagyan.com/javagyan/188061494527412080</id><published>2010-12-31T13:18:06.081Z</published><updated>2010-12-31T13:21:23.239Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-31T13:21:23.223Z</app:edited><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#announcement" label="announcement" /><title>Overloading can be tricky</title><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><table cellspacing="0" class="sites-layout-name-one-column sites-layout-hbox"><tbody><tr><td class="sites-layout-tile sites-tile-name-content-1"><div dir="ltr"><div><div class="sites-embed-align-right-wrapping-on"><div class="sites-embed-border-off sites-embed" style="width:300px;"><div class="sites-embed-content sites-embed-type-adsense"><script type="text/javascript"><!--
google_ad_client="pub-0373854690470703";
google_ad_host="pub-6693688277674466";
google_ad_width=300;
google_ad_height=250;
google_ad_format="300x250_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="000000";
google_color_url="0066CC";
google_color_text="000000";
//--></script><script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></div>Extra care must be taken while writing Overloading methods. The compiler decides which version of an overloaded method will be called based on declared compile-time type, not run-time type. For the case in which overloaded methods have the same number of arguments, the rules regarding this decision can sometimes be a bit tricky.</div><div><br /></div><div>If there may be confusion, you may simplify the design:</div><div><ul><li>use different method names, and avoid overloading altogether</li><li>retain overloading, but ensure each method has a distinct number of arguments</li><li>In addition, it is recommended that <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html" target="_blank">varargs </a>not be used when a method is overloaded, since this makes it more difficult to determine which overload is being called.</li></ul></div><div>Reminder : Overloading requires methods with distinct signatures. The signature of a method includes its name and the ordered list of its argument types. All other items appearing in a method header, such as exceptions, return type, final, and synchronized, do not contribute to a method's signature. </div></div></td></tr></tbody></table></div></content><link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/7590469474423082146" /><link rel="alternate" type="text/html" href="http://sites.google.com/a/javagyan.com/javagyan/useful-tips/overloadingcanbetricky" /><link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml" href="http://sites.google.com/feeds/revision/javagyan.com/javagyan/188061494527412080" /><link rel="self" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/188061494527412080" /><link rel="edit" type="application/atom+xml" href="http://sites.google.com/feeds/content/javagyan.com/javagyan/188061494527412080" /><author><name>Sanjeev Kumar</name><email>sanjeevonline@gmail.com</email></author><sites:pageName>overloadingcanbetricky</sites:pageName><sites:revision>1</sites:revision></entry></feed>
