<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-269081474586774736</atom:id><lastBuildDate>Thu, 29 Aug 2024 08:54:03 +0000</lastBuildDate><category>Array</category><category>Generic Revealed</category><category>Under The Hood</category><category>Interface in Java</category><category>About fail-fast nature of Iterator</category><category>Abstract Classes versus Interfaces</category><category>Finally. Use But Carefully</category><category>Java Collection UML Class Diagram</category><category>Java Data Types:</category><category>Polymorphism in Java</category><category>References in Java :</category><category>Secrets of HashSet Collection</category><category>Secrets of final keyword</category><category>Secrets of toString() method</category><category>What is an Object?</category><title>Java Discovery</title><description></description><link>http://javadiscovery.blogspot.com/</link><managingEditor>noreply@blogger.com (Anonymous)</managingEditor><generator>Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6430889122289392722</guid><pubDate>Sat, 24 Aug 2013 12:07:00 +0000</pubDate><atom:updated>2013-08-24T05:07:43.548-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Data Types:</category><title>Java Data Types:</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;The Java data types can be divided into a set of primitive types and a reference type. Variable of primitive types hold primitive values and variables of the reference type hold reference values. Reference values refer to objects, but are not objects themselves. Primitive values, by contrast &amp;nbsp;do not refer anything. They are actual data themselves.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Look at the diagram below :&lt;/b&gt;&lt;/div&gt;
&lt;/h4&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiwSDewunEjbOgLn9FGtAHDFyFvqaR75GP68sW0wkPgx3uLVhxVI0BkQarZgzKD30jLJorhEk092ombw9Gj3xlGd9JP7d0AahfgCU5hgWqdRFo5LvYvbSDxolfwmvVIQZC71OMJ7Jg0ps/s1600/diagram.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img alt=&quot;Java Data Types Diagram&quot; border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiwSDewunEjbOgLn9FGtAHDFyFvqaR75GP68sW0wkPgx3uLVhxVI0BkQarZgzKD30jLJorhEk092ombw9Gj3xlGd9JP7d0AahfgCU5hgWqdRFo5LvYvbSDxolfwmvVIQZC71OMJ7Jg0ps/s640/diagram.png&quot; title=&quot;Java Data Types &quot; width=&quot;587&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/java-data-types.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiwSDewunEjbOgLn9FGtAHDFyFvqaR75GP68sW0wkPgx3uLVhxVI0BkQarZgzKD30jLJorhEk092ombw9Gj3xlGd9JP7d0AahfgCU5hgWqdRFo5LvYvbSDxolfwmvVIQZC71OMJ7Jg0ps/s72-c/diagram.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6878156478674313828</guid><pubDate>Sat, 24 Aug 2013 10:58:00 +0000</pubDate><atom:updated>2013-08-24T05:07:30.986-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">References in Java :</category><title>References in Java :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Reference :&amp;nbsp;&lt;/b&gt; Reference is an abstract identifier for a block of memory in the heap.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Special Reference: &lt;/b&gt;There are three special references in Java source code.&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;this&lt;/li&gt;
&lt;li&gt;super&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;null reference :&lt;/b&gt; The &lt;i&gt;null&lt;/i&gt; reference is an invalid reference. It has no type , and may be assigned to a variable of any reference type. When a reference variable is declared but not constructed , it is initially equal to null.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;this reference :&lt;/b&gt; The &lt;i&gt;this&lt;/i&gt; reference always refers to the current object, method .&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Example: &amp;nbsp; &amp;nbsp; int j = this.x;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;super reference :&lt;/b&gt;&amp;nbsp;The &lt;i&gt;super &lt;/i&gt;reference refers to the methods and fields of the immediate &lt;i&gt;super class&lt;/i&gt;. You need to use super prefix only if a field or method in the subclass has same name as a field or method in super class.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
If &amp;nbsp;you use &lt;b&gt;super() &lt;/b&gt;as the first statement in a constructor, it calls the matching constructor in the immediate super class based on parameters passed to &lt;b&gt;super() &lt;/b&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKUlW-NdM7rrhclR6JWZDZY9OLAayb4OUTZsw3GUqDjAAqIrRcBrOXVqUD9WHwNntA1y2jAByWwNzLPooe51N9Af4Bh2E2mOc-_f44T30mUCOwAK6l3PNZgB_oF5ApMsvcTZIjavrczLc/s1600/important-icon-474.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;55px&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKUlW-NdM7rrhclR6JWZDZY9OLAayb4OUTZsw3GUqDjAAqIrRcBrOXVqUD9WHwNntA1y2jAByWwNzLPooe51N9Af4Bh2E2mOc-_f44T30mUCOwAK6l3PNZgB_oF5ApMsvcTZIjavrczLc/s1600/important-icon-474.jpg&quot; /&gt;&lt;/a&gt;If you do not include an explicit call to &lt;b&gt;super() &lt;/b&gt;as the first statement in your constructor, then the compiler will insert such a call into the byte code. The compiler always chooses to no &amp;nbsp;arguments &lt;b&gt;super() &lt;/b&gt;constructor if you don&#39;t explicitly choose a different one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; Example: &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;b&gt;public class SuperClass{&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;b&gt;public SuperClass(int i)&lt;/b&gt;&lt;b&gt;{&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;b&gt;class SubClass extends SuperClass{&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public SubClass(){&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp;}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Note: &lt;/b&gt;If you try to compile this program you get the error message &quot;no constructor matching SuperClass() found in class SuperClass. Because constructor chaining is mandatory in super-sub hierarchy.If you provide a parameterised constructor in super class then always put a noargs constructor in super class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/references-in-java.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKUlW-NdM7rrhclR6JWZDZY9OLAayb4OUTZsw3GUqDjAAqIrRcBrOXVqUD9WHwNntA1y2jAByWwNzLPooe51N9Af4Bh2E2mOc-_f44T30mUCOwAK6l3PNZgB_oF5ApMsvcTZIjavrczLc/s72-c/important-icon-474.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-8800826511556740266</guid><pubDate>Tue, 20 Aug 2013 02:34:00 +0000</pubDate><atom:updated>2013-08-19T19:39:15.182-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">About fail-fast nature of Iterator</category><title>About fail-fast nature of Iterator</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The iterators returned by this class&#39;s iterator and listIterator methods are fail-fast. If the list is &amp;nbsp;structurally modified at any time after the iterator is created, in any way &lt;b&gt;except through the Iterator&#39;s own remove or add methods&lt;/b&gt;, the iterator will throw a &lt;i&gt;ConcurrentModificationException&lt;/i&gt;. &amp;nbsp;Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;
&lt;b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Example:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;b&gt;List&amp;lt;String&amp;gt; staff = new ArrayList&amp;lt;String&amp;gt;();&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;staff.add(&quot;Ajay&quot;);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;staff.add(&quot;Kumar&quot;);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;staff.add(&quot;Singh&quot;);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;staff.add(&quot;sunny&quot;);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ListIterator listItr = staff.listIterator(); &amp;nbsp;&lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;//get the ListIterator&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #073763;&quot;&gt;//&amp;nbsp;after getting iterator&amp;nbsp;if we change(add or remove) here by ArrayList.add of ArrayList.remove then &amp;nbsp;it will throw a&amp;nbsp;&lt;i&gt;ConcurrentModificationException except ListIterator.add or ListIterotr.remove.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;color: #073763;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;staff.add(&quot;Ajay Kumar&quot;); &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #073763;&quot;&gt;//&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;will throw a&amp;nbsp;&lt;i&gt;ConcurrentModificationException&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;i style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/i&gt;&lt;b&gt;listItr.add(&quot;Ajay Kumar&quot;); &amp;nbsp; // &lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;OK&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
But there may be a exceptional situation where more than one thread of Iterator is trying to modify a collection in unsynchronized &amp;nbsp;block or method then it will throw&amp;nbsp;&lt;i&gt;ConcurrentModificationException.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp;ListIterator listItr = staff.listIterator(); &amp;nbsp;&lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;//get the ListIterator&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;b&gt;Iterator itr = staff.iterator(); &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;//get the Iterator&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;i style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/i&gt;&lt;b&gt;listItr.add(&quot;Ajay Kumar&quot;); &amp;nbsp; //&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;color: #073763;&quot;&gt;throw&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;color: #073763;&quot;&gt;ConcurrentModificationException&lt;/i&gt;&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/about-fail-fast-nature-of-iterator.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-4746448798676004414</guid><pubDate>Mon, 19 Aug 2013 17:29:00 +0000</pubDate><atom:updated>2013-08-19T10:42:22.282-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Secrets of HashSet Collection</category><title>Secrets of HashSet Collection :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;HashSet is data structures that let you find elements much faster. The drawback is that those data structures give you no control over the order in which the elements appear. It uses hash table, A hash table computes an integer, called the hash code, for each object. A hash code is an integer that is somehow derived from the instance fields of an object, preferably such that objects with different data yield different codes.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;In Java, hash tables are implemented as arrays of linked lists. Each list is called a bucket. To find the place of an object in the table, compute its hash code and reduce it modulus the total number of buckets.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;LoadFactor:&amp;nbsp;&lt;/span&gt;&lt;/b&gt;  The load factor determines when a hash table is rehashed. For example, if the load factor is 0.75 (which is the default) and the table is more than 75% full, then it is automatically rehashed, with twice as many buckets.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0b5394; font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;Internal architecture of HashSet :&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;This class implements the Set interface, backed by a hash table &lt;/span&gt;&lt;b style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;(actually a HashMap instance)&lt;/b&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;. It uses HashMap object internally in constructor.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;private transient HashMap&amp;lt;E,Object&amp;gt; map;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;public HashSet() {&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;        map = new HashMap&amp;lt;E,Object&amp;gt;();   &lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75). There are four more constructors of HashSet.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;It internally uses HashMap methods. See below&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;public boolean add(E e) {&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;return map.put(e, PRESENT)==null; &amp;nbsp; //calls put method of Map&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/secrets-of-hashset-collection.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6576946108278122139</guid><pubDate>Mon, 19 Aug 2013 00:56:00 +0000</pubDate><atom:updated>2013-08-18T19:26:52.043-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Collection UML Class Diagram</category><title>Java Collection UML Class Diagram</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.karambelkar.info/2012/06/java-collections-uml-class-diagrams.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;55px&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuos95xjDLa2FqtekUpDmM_IeuY0tnPX-iy9EzdbdARrZQPCH2nEtU9tbY9LKaOSe_sxSFcWP_oMKJk64kPWJAOCxj6J-IWSdgsW0uDPeRqFNF5tvqEsI692t07NbSmt6gKw_1Aoc7NZs/s1600/Arrow_Animated.gif&quot; /&gt;&lt;/a&gt;&lt;b&gt;&lt;span style=&quot;color: blue; font-family: Times, Times New Roman, serif;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;a href=&quot;http://www.karambelkar.info/2012/06/java-collections-uml-class-diagrams.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;Click Here...&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuos95xjDLa2FqtekUpDmM_IeuY0tnPX-iy9EzdbdARrZQPCH2nEtU9tbY9LKaOSe_sxSFcWP_oMKJk64kPWJAOCxj6J-IWSdgsW0uDPeRqFNF5tvqEsI692t07NbSmt6gKw_1Aoc7NZs/s1600/Arrow_Animated.gif&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/java-collection-uml-class-diagram.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuos95xjDLa2FqtekUpDmM_IeuY0tnPX-iy9EzdbdARrZQPCH2nEtU9tbY9LKaOSe_sxSFcWP_oMKJk64kPWJAOCxj6J-IWSdgsW0uDPeRqFNF5tvqEsI692t07NbSmt6gKw_1Aoc7NZs/s72-c/Arrow_Animated.gif" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-7755494252823671632</guid><pubDate>Thu, 15 Aug 2013 15:23:00 +0000</pubDate><atom:updated>2013-08-15T08:25:45.580-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Secrets of final keyword</category><title>Secrets of final keyword :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;In Java, you use the keyword final to denote a constant.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;final double PI=3.14;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The keyword final indicates that you can assign to the variable once, then its value is set&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;once and for all.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;Important on final keyword:&lt;/b&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;System.out is a final variable . It is declared in the System class as&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public final class System&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;. . .&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt; public final static PrintStream out = nullPrintStream();&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;. . .&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Since out has been declared as final, you cannot reassign another print stream to it:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;out = new PrintStream(. . .);&lt;/b&gt; &amp;nbsp; &amp;nbsp;&lt;i&gt; &lt;span style=&quot;color: #0b5394;&quot;&gt;// ERROR--out is final&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-26mwX54HOYlWFJj2kMiE6hF0ZExIo3TEXDhEFsmugC-YDUTEn2xgTF0s-o7i-cRwPCtg4GKlfvG3vF6cvfkyvfUolMGfuMDiw6RNgXLHrDqc1zktnRjIHLsGpOS7HgBOsPz35cjvuU/s1600/important-icon-474.jpg&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;55&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-26mwX54HOYlWFJj2kMiE6hF0ZExIo3TEXDhEFsmugC-YDUTEn2xgTF0s-o7i-cRwPCtg4GKlfvG3vF6cvfkyvfUolMGfuMDiw6RNgXLHrDqc1zktnRjIHLsGpOS7HgBOsPz35cjvuU/s1600/important-icon-474.jpg&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;If you look at the System class, you will notice a method &lt;b&gt;setOut&lt;/b&gt; that lets you set &lt;b&gt;System.out&lt;/b&gt; to a different stream. You may wonder how that method can change the value of a final variable. However, the &lt;b&gt;setOut&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;method is a static method which calls native method, not implemented in the Java programming language.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp;&lt;/span&gt; &amp;nbsp;public static void setOut(PrintStream out) {&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; checkIO();&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; setOut0(out); //This is a native method&amp;nbsp;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; }&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;i&gt;private static native void setOut0(PrintStream out);&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;Native methods can bypass the access control mechanisms of the Java language.&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/secrets-of-final-keyword.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-26mwX54HOYlWFJj2kMiE6hF0ZExIo3TEXDhEFsmugC-YDUTEn2xgTF0s-o7i-cRwPCtg4GKlfvG3vF6cvfkyvfUolMGfuMDiw6RNgXLHrDqc1zktnRjIHLsGpOS7HgBOsPz35cjvuU/s72-c/important-icon-474.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-4599961835622948558</guid><pubDate>Thu, 15 Aug 2013 14:56:00 +0000</pubDate><atom:updated>2013-08-15T07:56:31.603-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Finally. Use But Carefully</category><title>Finally Block. Use But Carefully</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Finally clause is a block that guarantees that this block will be executed whether there is exception&amp;nbsp;occurred in try-catch block on not. If we call system.exit(0) then finally block will not be executed.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;You can use the finally clause without a catch clause. For example, consider the following&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;try statement:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;Graphics g = image.getGraphics();&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;code that might&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;throw exceptions&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;finally&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;g.dispose();&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The g.dispose() command in the finally clause is executed whether or not an exception is&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;encountered in the try block.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;Point to remember using finally block:&lt;/b&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The finally clause leads to unexpected control flow when you exit the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;middle of a try block with a return statement. Before the method&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;returns, the contents of the finally block is executed. If it also contains&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;a return statement, then it masks the original return value.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Consider this&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;example:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static int f(int n)&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;int r = n * n;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;return r;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;finally&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;if (n == 2) return 0;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;If you call f(2), then the try block computes r = 4 and executes&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;the return statement. However, the finally clause is executed before&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;the method actually returns. The finally clause causes the method to&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;return 0, ignoring the original return value of 4.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Sometimes the finally clause gives you grief, namely if the cleanup method can also throw&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;an exception. A typical case is closing a stream.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Suppose you want to make sure that you close a stream when an exception hits in&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;the stream processing code.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;InputStream in;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;code that might&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;throw exceptions&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;catch (IOException e)&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;show error dialog&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;finally&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;in.close();&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Now suppose that the code in the try block throws some exception other than an&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;IOException that is of interest to the caller of the code. The finally block executes, and the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;close method is called. That method can itself throw an IOException! When it does, then&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;the original exception is lost and the IOException is thrown instead. That is very much&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;against the spirit of exception handling.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/finally-block-use-but-carefully.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-4369723467995486844</guid><pubDate>Fri, 09 Aug 2013 11:10:00 +0000</pubDate><atom:updated>2013-08-09T04:15:26.819-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Secrets of toString() method</category><title>Secrets of toString() method:</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; toString() method that is declared and implemented in Object class that  returns  a string representation of the object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;whenever an object is concatenated with a string, using the “+” operator, the Java compiler automatically invokes the toString method to obtain a string representation of the object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;  For example:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Point p = new Point(10, 20);&lt;br /&gt;&lt;br /&gt;String message = &quot;The current position is &quot; + p;  // automatically invokes p.toString()&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;If x is any object and you call&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;System.out.println(x);&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;br /&gt;then the println method simply calls x.toString() and prints the resulting string.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Times, Times New Roman, serif;&quot;&gt; Object&#39;s toString method:&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;The Object class defines the toString method to print the class name and the memory&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;location of the object. For example, the call &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; public String toString() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         return getClass().getName() + &quot;@&quot; + Integer.toHexString(hashCode());&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;b style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;}&lt;/b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  For example, the call:&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;System.out.println(System.out);&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; produces an output that looks like this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; java.io.PrintStream@2f6684&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; The reason is that the implementor of the PrintStream class didn&#39;t bother to override the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;toString method.&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;br /&gt; &lt;/span&gt; &lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/secrets-of-tostring-method.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-8459047698859466550</guid><pubDate>Fri, 02 Aug 2013 10:47:00 +0000</pubDate><atom:updated>2013-08-02T18:34:21.958-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Polymorphism in Java</category><title>Polymorphism in Java </title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;em style=&quot;background-color: white; color: #212324; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;index-polymorphism-definition&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/em&gt;
&lt;em style=&quot;background-color: white; color: #212324; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;index-polymorphism-definition&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/em&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Polymorphism, which &amp;nbsp;means &quot;many forms,&quot; is the ability to treat an object of any subclass of a base class as if it were an object of the base class Object variables are polymorphic. &lt;b&gt;A variable of type&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;Employee can refer to an object of type Employee or an object of any subclass of the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;Employee class (such as Manager, Executive, Secretary, and so on).&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;A base class has, therefore, many forms: the base class itself, and any of its subclasses.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKiDoFVhQYB7XeMx8E3070NXOoBdLLItqK-VMpPbS03KunRq7yLD1eOmFHcoliYD6RqFSfdOMHiWHmbU8y3mk5wq7Sh_vkyCugEv5ZIpd1G9suflXFdJNj1BQCTsZJLIUkd4rYvZvZJ9s/s1600/inheFig3.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;145&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKiDoFVhQYB7XeMx8E3070NXOoBdLLItqK-VMpPbS03KunRq7yLD1eOmFHcoliYD6RqFSfdOMHiWHmbU8y3mk5wq7Sh_vkyCugEv5ZIpd1G9suflXFdJNj1BQCTsZJLIUkd4rYvZvZJ9s/s320/inheFig3.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Liquid myFavoriteBeverage = new Liquid();&lt;br /&gt; &lt;br /&gt;The myFavoriteBeverage variable holds a reference to a Liquid object. This is a sensible arrangement; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; however, there is another possibility brought to you courtesy of polymorphism. Because of polymorphism, you can assign a reference to any object that is-a Liquid to a variable of type Liquid. So, assuming the inheritance hierarchy shown in Figure 8-1, either of the following assignments will also work:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Liquid &amp;nbsp;myFavoriteBeverage = new Coffee();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;// or... Liquid &amp;nbsp;myFavoriteBeverage = new Milk();&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Therefore, you can sprinkle some polymorphism in your Java program simply by using a variable with a base type to hold a reference to an object of a derived type.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To fully realize the wonders of polymorphism, you must send a message to an object without knowing the actual class of the object. To do this in Java, you just invoke a method defined in a base type on an object referenced by a variable of the base type. As you saw above, the object referred to by a base class reference might be of the base class or any of its subclasses. Therefore, when you write the code to invoke the method, you don&#39;t necessarily know the actual class of the object. Likewise, when you compile the code, the compiler doesn&#39;t necessarily know the actual class of the object. At run-time, the Java Virtual Machine determines the actual class of the object each time the method invocation is requested by your program. Based on this information, the Java Virtual Machine invokes the method implementation belonging to the object&#39;s actual class. Letting the Java Virtual Machine determine which method implementation to invoke, based on the actual class of the object, is how you realize the full power of polymorphism in your programs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;public class TestBikes {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt; public static void main(String[] args){&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Bicycle bike01, bike02, bike03;
 bike01 = new Bicycle(20, 10, 1); //Polymorphism too.&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bike02 = new MountainBike(20, 10, 5, &quot;Dual&quot;); &amp;nbsp; &amp;nbsp;//Polymorphism&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bike03 = new RoadBike(40, 20, 8, 23);
 bike01.printDescription(); //Polymorphism&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bike02.printDescription(); &amp;nbsp; &amp;nbsp; &amp;nbsp; //Dynamic Dispatch&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bike03.printDescription(); &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;//Dynamic Dispatch&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt; }&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;Confusion at polymorphism :&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;There is no any run-time polymorphism because polymorphism is achieved at run-time automatically by the JVM, if not then it is not polymorphism.&lt;/li&gt;
&lt;li&gt;Method overriding is always connected to polymorphism but this is false (source: oracle.com). But is used to get full benefits of polymorphism and dynamic dispatch.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/polymorphism-in-java.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKiDoFVhQYB7XeMx8E3070NXOoBdLLItqK-VMpPbS03KunRq7yLD1eOmFHcoliYD6RqFSfdOMHiWHmbU8y3mk5wq7Sh_vkyCugEv5ZIpd1G9suflXFdJNj1BQCTsZJLIUkd4rYvZvZJ9s/s72-c/inheFig3.gif" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-4884231961512907129</guid><pubDate>Fri, 02 Aug 2013 05:08:00 +0000</pubDate><atom:updated>2013-08-02T01:17:39.483-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Abstract Classes versus Interfaces</category><title>Abstract Classes versus Interfaces :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain&lt;br /&gt;
implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.&lt;br /&gt;
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable orCloneable, for example.&lt;br /&gt;
By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;When an Abstract Class Implements an Interface&lt;/span&gt;&lt;/h3&gt;
&lt;br /&gt;
&lt;br /&gt;
In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface&#39;s methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract. For example, &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
abstract class X implements Y {&lt;br /&gt;
&lt;br /&gt;
// implements all but one method of Y&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class XX extends X {&lt;br /&gt;
&lt;br /&gt;
// implements the remaining method in Y&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/abstract-classes-versus-interfaces.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-3466472218389051973</guid><pubDate>Fri, 02 Aug 2013 04:11:00 +0000</pubDate><atom:updated>2013-08-01T21:13:51.851-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">What is an Object?</category><title>What Is an Object?</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
Real-world objects share two characteristics: They all have&lt;b&gt;&amp;nbsp;&lt;i&gt;state&lt;/i&gt;&amp;nbsp;&lt;/b&gt;and&amp;nbsp;&lt;i&gt;&lt;b&gt;behavior&lt;/b&gt;&lt;/i&gt;. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in&amp;nbsp;&lt;i&gt;&lt;b&gt;fields&lt;/b&gt;&lt;/i&gt;&amp;nbsp;(variables in some programming languages) and exposes its behavior through&lt;b&gt;&amp;nbsp;&lt;i&gt;methods&lt;/i&gt;&lt;/b&gt;&amp;nbsp;(functions in some programming languages). Methods operate on an object&#39;s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object&#39;s methods is known as&amp;nbsp;&lt;i&gt;&lt;b&gt;data encapsulation&lt;/b&gt;&amp;nbsp;&lt;/i&gt;a fundamental principle of object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Object provides many benefits:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.&lt;/li&gt;
&lt;li&gt;Information-hiding: By interacting only with an object&#39;s methods, the details of its internal implementation remain hidden from the outside world.&lt;/li&gt;
&lt;li&gt;Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.&lt;/li&gt;
&lt;li&gt;Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace&amp;nbsp;&lt;i&gt;it&lt;/i&gt;, not the entire machine.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/what-is-object.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-3934493274347193430</guid><pubDate>Thu, 01 Aug 2013 17:12:00 +0000</pubDate><atom:updated>2013-08-01T10:16:36.681-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interface in Java</category><title>Rewriting Interfaces :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Consider an interface that you have developed called&amp;nbsp;&lt;code&gt;DoIt&lt;/code&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public interface DoIt {
   void doSomething(int i, double x);
   int doSomethingElse(String s);
}
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Suppose that, at a later time, you want to add a third method to&amp;nbsp;&lt;code&gt;DoIt&lt;/code&gt;, so that the interface now becomes:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public interface DoIt {

   void doSomething(int i, double x);
   int doSomethingElse(String s);
   boolean didItWork(int i, double x, String s);
   
}
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;If you make this change, all classes that implement the old&amp;nbsp;&lt;code&gt;DoIt&lt;/code&gt;&amp;nbsp;interface will break because they don&#39;t implement the interface anymore. Programmers relying on this interface will protest loudly.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Try to anticipate all uses for your interface and to specify it completely from the beginning. Given that this is often impossible, you may need to create more interfaces later. For example, you could create a&amp;nbsp;&lt;code&gt;DoItPlus&lt;/code&gt;&amp;nbsp;interface that extends&amp;nbsp;&lt;code&gt;DoIt&lt;/code&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public interface DoItPlus extends DoIt {

   boolean didItWork(int i, double x, String s);
   
}
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Now users of your code can choose to continue to use the old interface or to upgrade to the new interface.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8X3o2aPicYu5rqVkMUN_bK3Q3o0WvFlq4AFFhpWt0MC88RfvTSTkmJpKXwUOu_DYLZw9m0FOACyF87-sj3atiGeuRgat2Ftk6P_WIpJ8CEWYdby5WJRIN526AxSz0DJipfCnB5mVbCTk/s1600/important-icon-474.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;55&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8X3o2aPicYu5rqVkMUN_bK3Q3o0WvFlq4AFFhpWt0MC88RfvTSTkmJpKXwUOu_DYLZw9m0FOACyF87-sj3atiGeuRgat2Ftk6P_WIpJ8CEWYdby5WJRIN526AxSz0DJipfCnB5mVbCTk/s1600/important-icon-474.jpg&quot; /&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;nbsp;An interface defines a protocol of communication between two objects.&lt;/li&gt;
&lt;li&gt;An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.&lt;/li&gt;
&lt;li&gt;A class that implements an interface must implement all the methods declared in the interface.&lt;/li&gt;
&lt;li&gt;An interface name can be used anywhere a type can be used.&lt;/li&gt;
&lt;/ul&gt;
&lt;/span&gt;&lt;br /&gt;

&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/rewriting-interfaces.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8X3o2aPicYu5rqVkMUN_bK3Q3o0WvFlq4AFFhpWt0MC88RfvTSTkmJpKXwUOu_DYLZw9m0FOACyF87-sj3atiGeuRgat2Ftk6P_WIpJ8CEWYdby5WJRIN526AxSz0DJipfCnB5mVbCTk/s72-c/important-icon-474.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6933048326078956157</guid><pubDate>Thu, 01 Aug 2013 17:09:00 +0000</pubDate><atom:updated>2013-08-01T10:09:53.048-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interface in Java</category><title>Interface in Java</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
&lt;br /&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;As you&#39;ve already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object&#39;s&amp;nbsp;&lt;i&gt;interface&lt;/i&gt;&amp;nbsp;with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the &quot;power&quot; button to turn the television on and off.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;In its most common form, an interface is a group of related methods with empty bodies. A bicycle&#39;s behavior, if specified as an interface, might appear as follows:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as&amp;nbsp;&lt;code&gt;HeroBicycle&lt;/code&gt;), and you&#39;d use the&amp;nbsp;&lt;code&gt;implements&amp;nbsp;&lt;/code&gt;keyword in the class declaration:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;class HeroBicycle &lt;strong&gt;implements&lt;/strong&gt; Bicycle {

    // remainder of this class 
    // implemented as before
}
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will su&lt;/span&gt;ccessfully compile.&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/interface-in-java.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6340530604037159755</guid><pubDate>Thu, 01 Aug 2013 10:39:00 +0000</pubDate><atom:updated>2013-08-01T05:11:49.290-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Generic Revealed</category><title>Restrictions on Generics :</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;font-family: sans-serif; font-size: 17px;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;instantiate&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;h2 style=&quot;font-family: sans-serif; font-size: 17px;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;instantiate&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;h2 style=&quot;font-family: sans-serif; font-size: 17px;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;instantiate&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;h2 style=&quot;font-family: sans-serif; font-size: 17px;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;instantiate&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;instantiate&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Cannot Instantiate Generic Types with Primitive Types:&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Consider the following parameterized type:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;class Pair&amp;lt;K, V&amp;gt; {&lt;br /&gt;
    private K key;&lt;br /&gt;    private V value;&lt;br /&gt;
    public Pair(K key, V value) {&lt;br /&gt;        this.key = key;&lt;br /&gt;        this.value = value;&lt;br /&gt;    }&lt;br /&gt;
    // ...&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;Pair&amp;lt;&lt;strong&gt;int, char&lt;/strong&gt;&amp;gt; p = new Pair&amp;lt;&amp;gt;(8, &#39;a&#39;);  // compile-time error&lt;br /&gt;Pair&amp;lt;&lt;strong&gt;Integer, Character&lt;/strong&gt;&amp;gt; p = new Pair&amp;lt;&amp;gt;(8, &#39;a&#39;);&lt;br /&gt;Pair&amp;lt;Integer, Character&amp;gt; p = new Pair&amp;lt;&amp;gt;(Integer.valueOf(8), new Character(&#39;a&#39;));&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;When creating a&amp;nbsp;&lt;tt&gt;Pair&lt;/tt&gt;&amp;nbsp;object, you cannot subsitute a primitive type for the type parameter&amp;nbsp;&lt;tt&gt;K&lt;/tt&gt;&amp;nbsp;or&amp;nbsp;&lt;tt&gt;V&lt;/tt&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;You can substitute only non-primitive types for the type parameters&amp;nbsp;&lt;tt&gt;K&lt;/tt&gt;&amp;nbsp;and&amp;nbsp;&lt;tt&gt;V&lt;/tt&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Note that the Java compiler autoboxes&amp;nbsp;&lt;tt&gt;8&lt;/tt&gt;&amp;nbsp;to&amp;nbsp;&lt;tt&gt;Integer.valueOf(8)&lt;/tt&gt;&amp;nbsp;and &#39;&lt;tt&gt;a&lt;/tt&gt;&#39; to&amp;nbsp;&lt;tt&gt;Character(&#39;a&#39;).&lt;/tt&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;createStatic&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;Cannot Declare Static Fields Whose Types are Type Parameters&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public class MobileDevice&amp;lt;T&amp;gt; {&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;    private static T os;&lt;/span&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;    // ...&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;MobileDevice&amp;lt;Smartphone&amp;gt; phone = new MobileDevice&amp;lt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;MobileDevice&amp;lt;Pager&amp;gt; pager = new MobileDevice&amp;lt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;MobileDevice&amp;lt;TabletPC&amp;gt; pc = new MobileDevice&amp;lt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;A class&#39;s static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are not allowed. Consider the following class:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;If static fields of type parameters were allowed, then the following code would be confused:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Because the static field&amp;nbsp;&lt;tt&gt;os&lt;/tt&gt;&amp;nbsp;is shared by&amp;nbsp;&lt;tt&gt;phone&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;pager&lt;/tt&gt;, and&amp;nbsp;&lt;tt&gt;pc&lt;/tt&gt;, what is the actual type of&amp;nbsp;&lt;tt&gt;os&lt;/tt&gt;? It cannot be&amp;nbsp;&lt;tt&gt;Smartphone&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;Pager&lt;/tt&gt;, and&amp;nbsp;&lt;tt&gt;TabletPC&lt;/tt&gt;&amp;nbsp;at the same time. You cannot, therefore, create static fields of type parameters.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;createArrays&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;Cannot Create Arrays of Parameterized Types:&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;You cannot create arrays of parameterized types. For example, the following code does not compile:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;List&amp;lt;Integer&amp;gt;[] arrayOfLists = new List&amp;lt;Integer&amp;gt;[2];  // compile-time error&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=269081474586774736&quot; name=&quot;cannotCatch&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;Cannot Create, Catch, or Throw Objects of Parameterized Types :&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;A generic class cannot extend the&amp;nbsp;&lt;tt&gt;Throwable&lt;/tt&gt;&amp;nbsp;class directly or indirectly. For example, the following classes will not compile:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;// Extends Throwable indirectly
class MathException&amp;lt;T&amp;gt; extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException&amp;lt;T&amp;gt; extends Throwable { /* ... */ // compile-time error
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;A method cannot catch an instance of a type parameter:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static &amp;lt;T extends Exception, J&amp;gt; void execute(List&amp;lt;J&amp;gt; jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;You can, however, use a type parameter in a&amp;nbsp;&lt;tt&gt;throws&lt;/tt&gt;&amp;nbsp;clause:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;class Parser&amp;lt;T extends Exception&amp;gt; {
    public void parse(File file) throws T {     // OK
        // ...
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/span&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/restrictions-on-generics.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-5885483382600164719</guid><pubDate>Thu, 01 Aug 2013 09:59:00 +0000</pubDate><atom:updated>2013-08-01T05:12:29.036-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Generic Revealed</category><title>Wildcards in Generics</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;In generic code, the question mark (&lt;tt&gt;?&lt;/tt&gt;), called the&amp;nbsp;&lt;em&gt;wildcard&lt;/em&gt;, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;h1 style=&quot;font-size: 20px;&quot;&gt;
&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;Upper Bounded Wildcards:&lt;/span&gt;&lt;/h1&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;You can use an upper bounded wildcard to relax the restrictions on a variable. For example, say you want to write a method that works on&amp;nbsp;&lt;tt&gt;List&amp;lt;Integer&amp;gt;&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;List&amp;lt;Double&amp;gt;&lt;/tt&gt;,&lt;em&gt;and&lt;/em&gt;&amp;nbsp;&lt;tt&gt;List&amp;lt;Number&amp;gt;&lt;/tt&gt;; you can achieve this by using an upper bounded wildcard.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;To declare an upper-bounded wildcard, use the wildcard character (&#39;&lt;tt&gt;?&lt;/tt&gt;&#39;), followed by the&amp;nbsp;&lt;tt&gt;&lt;b&gt;extends&lt;/b&gt;&lt;/tt&gt;&amp;nbsp;keyword, followed by its&amp;nbsp;&lt;em&gt;upper bound&lt;/em&gt;. Note that, in this context,&amp;nbsp;&lt;tt&gt;extends&amp;nbsp;&lt;/tt&gt;is used in a &amp;nbsp;sense to mean either &quot;extends&quot; (as in classes) or &quot;implements&quot; (as in interfaces).&amp;nbsp;To write the method that works on lists of&amp;nbsp;&lt;tt&gt;Number&lt;/tt&gt;&amp;nbsp;and the sub-types of&amp;nbsp;&lt;tt&gt;Number&lt;/tt&gt;, such as&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;Double&lt;/tt&gt;, and&amp;nbsp;&lt;tt&gt;Float&lt;/tt&gt;, you would specify&amp;nbsp;&lt;tt&gt;List&amp;lt;? extends Number&amp;gt;&lt;/tt&gt;. The term&amp;nbsp;&lt;tt&gt;List&amp;lt;Number&amp;gt;&lt;/tt&gt;&amp;nbsp;is more restrictive than&amp;nbsp;&lt;tt&gt;List&amp;lt;? extends Number&amp;gt;&lt;/tt&gt;&amp;nbsp;because the former matches a list of type&amp;nbsp;&lt;tt&gt;Number&lt;/tt&gt;&amp;nbsp;only, whereas the latter matches a list of type&amp;nbsp;&lt;tt&gt;Number&lt;/tt&gt;&amp;nbsp;or any of its subclasses.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
Consider the following&amp;nbsp;&lt;tt&gt;process&lt;/tt&gt;&amp;nbsp;method:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static void process(List&lt;strong&gt;&amp;lt;? extends Test&amp;gt;&lt;/strong&gt; list) { /* ... */ }&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The upper bounded wildcard,&amp;nbsp;&lt;tt&gt;&amp;lt;? extends Test&amp;gt;&lt;/tt&gt;, where&amp;nbsp;&lt;tt&gt;Test&lt;/tt&gt;&amp;nbsp;is any type, matches&amp;nbsp;&lt;tt&gt;Test&lt;/tt&gt;&amp;nbsp;and any subtype of Test. The&amp;nbsp;&lt;tt&gt;process&lt;/tt&gt;&amp;nbsp;method can access the list elements as type Test:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static void process(List&amp;lt;? extends Test&amp;gt; list) {
    for (Test var : list) {
        // ...
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-size: 20px;&quot;&gt;
&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;Unbounded Wildcards:&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The unbounded wildcard type is specified using the wildcard character (&lt;tt&gt;?&lt;/tt&gt;), for example,&amp;nbsp;&lt;tt&gt;List&amp;lt;?&amp;gt;&lt;/tt&gt;. This is called a&amp;nbsp;&lt;em&gt;&lt;b&gt;&lt;u&gt;list of unknown type&lt;/u&gt;&lt;/b&gt;&lt;/em&gt;. There are two scenarios where an unbounded wildcard is a useful approach:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;If you are writing a method that can be implemented using functionality provided in the&amp;nbsp;&lt;tt&gt;Object&lt;/tt&gt;&amp;nbsp;class.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;When the code is using methods in the generic class that don&#39;t depend on the type parameter. For example,&amp;nbsp;&lt;tt&gt;List.size&lt;/tt&gt;&amp;nbsp;or&amp;nbsp;&lt;tt&gt;List.clear&lt;/tt&gt;. In fact,&amp;nbsp;&lt;tt&gt;Class&amp;lt;?&amp;gt;&lt;/tt&gt;&amp;nbsp;is so often used because most of the methods in&amp;nbsp;&lt;tt&gt;Class&amp;lt;T&amp;gt;&lt;/tt&gt;&amp;nbsp;do not depend on&amp;nbsp;&lt;tt&gt;T&lt;/tt&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;Consider the following method,&amp;nbsp;&lt;tt&gt;printList&lt;/tt&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static void printList(List&amp;lt;Object&amp;gt; list) {
    for (Object elem : list)
        System.out.println(elem + &quot; &quot;);
    System.out.println();
}
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The goal of&amp;nbsp;&lt;tt&gt;printList&lt;/tt&gt;&amp;nbsp;is to print a list of any type, but it fails to achieve that goal — it prints only a list of&amp;nbsp;&lt;tt&gt;Object&lt;/tt&gt;&amp;nbsp;instances; it cannot print&amp;nbsp;&lt;tt&gt;List&amp;lt;Integer&amp;gt;&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;List&amp;lt;String&amp;gt;&lt;/tt&gt;,&lt;tt&gt;List&amp;lt;Double&amp;gt;&lt;/tt&gt;, and so on, because they are not subtypes of&amp;nbsp;&lt;tt&gt;List&amp;lt;Object&amp;gt;&lt;/tt&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;To write a generic&amp;nbsp;&lt;tt&gt;printList&lt;/tt&gt;&amp;nbsp;method, use&amp;nbsp;&lt;tt&gt;List&amp;lt;?&amp;gt;&lt;/tt&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static void printList(List&amp;lt;?&amp;gt; list) {
    for (Object elem: list)
        System.out.print(elem + &quot; &quot;);
    System.out.println();
}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKH1DOb_gybkbGKcFKG_U6Ge76olyFFyK3wwaMurW8aJextugsJx91T1PhmqwjSzR6p0VF3PAHwSDEh5iuENQZM20dVqBpCbZZ4t9pgYHC45DJEzZyU7uSsFLWihki-68AAPz1_uI8aio/s1600/important-icon-474.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;60px&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKH1DOb_gybkbGKcFKG_U6Ge76olyFFyK3wwaMurW8aJextugsJx91T1PhmqwjSzR6p0VF3PAHwSDEh5iuENQZM20dVqBpCbZZ4t9pgYHC45DJEzZyU7uSsFLWihki-68AAPz1_uI8aio/s1600/important-icon-474.jpg&quot; style=&quot;cursor: move;&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;It&#39;s important to note that&amp;nbsp;&lt;tt&gt;List&amp;lt;Object&amp;gt;&lt;/tt&gt;&amp;nbsp;and&amp;nbsp;&lt;tt&gt;List&amp;lt;?&amp;gt;&lt;/tt&gt;&amp;nbsp;are not the same. You can insert an&amp;nbsp;&lt;tt&gt;Object&lt;/tt&gt;, or any subtype of&amp;nbsp;&lt;tt&gt;Object&lt;/tt&gt;, into a&amp;nbsp;&lt;tt&gt;List&amp;lt;Object&amp;gt;&lt;/tt&gt;. But you can only insert&amp;nbsp;&lt;tt&gt;null&lt;/tt&gt;&amp;nbsp;into a&amp;nbsp;&lt;tt&gt;List&amp;lt;?&amp;gt;.&lt;/tt&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-size: 20px;&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;b&gt;Lower Bounded Wildcards:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-size: 20px;&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&amp;nbsp;A&amp;nbsp;&lt;em&gt;&lt;b&gt;lower bounded&lt;/b&gt;&lt;/em&gt;&amp;nbsp;wildcard restricts the unknown type to be a specific type or a&amp;nbsp;&lt;em&gt;super type&lt;/em&gt;&amp;nbsp;of that type.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;A lower bounded wildcard is expressed using the wildcard character (&#39;&lt;tt&gt;?&lt;/tt&gt;&#39;), following by the&amp;nbsp;&lt;tt&gt;super&lt;/tt&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;keyword, followed by its&amp;nbsp;&lt;em&gt;lower bound&lt;/em&gt;:&amp;nbsp;&lt;tt&gt;&amp;lt;? super A&amp;gt;&lt;/tt&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;To write the method that works on lists of&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;&amp;nbsp;and the supertypes of&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;, such as&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;,&amp;nbsp;&lt;tt&gt;Number&lt;/tt&gt;, and&amp;nbsp;&lt;tt&gt;Object&lt;/tt&gt;, you would specify&amp;nbsp;&lt;tt&gt;List&amp;lt;? super Integer&amp;gt;&lt;/tt&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The term&amp;nbsp;&lt;tt&gt;List&amp;lt;Integer&amp;gt;&lt;/tt&gt;&amp;nbsp;is more restrictive than&amp;nbsp;&lt;tt&gt;List&amp;lt;? super Integer&amp;gt;&lt;/tt&gt;&amp;nbsp;because the former matches a list of type&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;&amp;nbsp;only, whereas the latter matches a list of any type that is a supertype of&amp;nbsp;&lt;tt&gt;Integer&lt;/tt&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;The following code adds the numbers 1 through 10 to the end of a list:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;public static void addNumbers(List&amp;lt;? super Integer&amp;gt; list) {
    for (int i = 1; i &amp;lt;= 10; i++) {
        list.add(i);
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-size: 20px;&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;font-size: 20px;&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKH1DOb_gybkbGKcFKG_U6Ge76olyFFyK3wwaMurW8aJextugsJx91T1PhmqwjSzR6p0VF3PAHwSDEh5iuENQZM20dVqBpCbZZ4t9pgYHC45DJEzZyU7uSsFLWihki-68AAPz1_uI8aio/s1600/important-icon-474.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;60px&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKH1DOb_gybkbGKcFKG_U6Ge76olyFFyK3wwaMurW8aJextugsJx91T1PhmqwjSzR6p0VF3PAHwSDEh5iuENQZM20dVqBpCbZZ4t9pgYHC45DJEzZyU7uSsFLWihki-68AAPz1_uI8aio/s1600/important-icon-474.jpg&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 20px;&quot;&gt;&lt;span style=&quot;color: #3d85c6; font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;NavBit&quot; style=&quot;padding: 4px 5px 0.5em 0px;&quot;&gt;
&lt;span style=&quot;color: #cc0000; font-size: 20px;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-size: 20px;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-size: 20px;&quot;&gt;&lt;span style=&quot;font-family: Times, Times New Roman, serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-family: Times, Times New Roman, serif;&quot;&gt;You can specify upper bound for a wild card, or you can specify lower bound, but you can not specify both.&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Times, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/wildcards-in-generics.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKH1DOb_gybkbGKcFKG_U6Ge76olyFFyK3wwaMurW8aJextugsJx91T1PhmqwjSzR6p0VF3PAHwSDEh5iuENQZM20dVqBpCbZZ4t9pgYHC45DJEzZyU7uSsFLWihki-68AAPz1_uI8aio/s72-c/important-icon-474.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-8425614067532172784</guid><pubDate>Thu, 01 Aug 2013 09:12:00 +0000</pubDate><atom:updated>2013-08-01T05:12:54.379-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Generic Revealed</category><title>Benefit of using Generics in Java</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&amp;nbsp;Generics enable&amp;nbsp;&lt;b&gt;&lt;em&gt;types&lt;/em&gt;&amp;nbsp;&lt;/b&gt;(classes and interfaces) to be parameters when defining classes, interfaces and methods.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code that uses generics has many benefits over non-generic code:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Stronger type checks at compile time.&lt;br /&gt;A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing run-time errors, which can be difficult to find.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Elimination of casts.&lt;br /&gt;The following code snippet without generics requires casting:&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;List list = new ArrayList();
list.add(&quot;hello&quot;);
String s = &lt;strong&gt;(String)&lt;/strong&gt; list.get(0);
&lt;/pre&gt;
&lt;/div&gt;
When re-written to use generics, the code does not require casting:&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;List&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();
list.add(&quot;hello&quot;);
String s = list.get(0);   // no cast&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/08/benefit-of-using-generics-in-java.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-1277244729295343498</guid><pubDate>Thu, 01 Aug 2013 02:03:00 +0000</pubDate><atom:updated>2013-08-01T05:13:54.683-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Array</category><title>Java Array Revealed_2</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
In Java array of characters is not a String :&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
In Java programming language an array of char is &amp;nbsp;neither a String nor an array of char is terminated by &#39;\u0000&#39; &amp;nbsp;(the NULL character).&lt;/div&gt;
&lt;div&gt;
A String object is immutable, that is, its contents never change, while an array of&lt;/div&gt;
&lt;div&gt;
char has mutable elements,But the length of array is final.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/java-array-revealed2.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-6656809108084028722</guid><pubDate>Thu, 01 Aug 2013 01:56:00 +0000</pubDate><atom:updated>2013-08-01T05:14:29.325-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Array</category><title>Java Array Revealed_1</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
IN the Java programming language, arrays are objects ,and &amp;nbsp;dynamically&lt;br /&gt;
created, and may be assigned to variables of type Object. All methods of&lt;br /&gt;
class Object may be invoked on an array.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Let&#39;s talk about Array variables:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
A variable of array type holds a reference to an object. Declaring a variable of array&lt;br /&gt;
type does not create an array object or allocate any space for array components. It&lt;br /&gt;
&lt;br /&gt;
creates only the variable itself, which can contain a reference to an array.&lt;br /&gt;
And interesting thing is about the array declaration style.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;byte[ ] set, get, wet[ ];&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;This declaration is equivalent to:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;byte set[ ], get[ ], wet[ ] [ ];&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;It is not recommend &amp;nbsp;&quot;&lt;i&gt;&lt;u&gt;mixed notation&lt;/u&gt;&lt;/i&gt;&quot; in an array variable declaration, where&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;brackets appear on both the type and in declarators&lt;/b&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/java-array-revealed1.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-7386396348770570103</guid><pubDate>Wed, 31 Jul 2013 03:05:00 +0000</pubDate><atom:updated>2013-08-01T05:14:43.096-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Array</category><title>Array Mutability</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Just as with object mutability, you need to be aware of array mutability. First look this example below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;public class ShippingInfo {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;private static final String[] states = {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&quot;AK&quot;, &quot;AZ&quot;, &quot;CA&quot;, &quot;DE&quot;, &quot;NV&quot;, &quot;NY&quot;};&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;}&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The following code fragment could be used to iterate through the list of states and print them out.&lt;br /&gt;
&lt;b&gt;for (int i = 0; i &amp;lt; ShippingInfo.states.length; i++) {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;System.out.println(ShippingInfo.states[i]);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;}&lt;/b&gt;&lt;br /&gt;
This is easy enough, but using the final keyword with arrays can be tricky. The following code does not&lt;br /&gt;
compile as you might expect.&lt;br /&gt;
ShippingInfo.states = new String[50];&lt;br /&gt;
It fails because you cannot assign values to final variables. The following code, however, is perfectly legal:&lt;br /&gt;
ShippingInfo.states[5] = &quot;Java City&quot;;&lt;br /&gt;
This code replaces the entry for &amp;nbsp;(&quot;NV&quot;) with &quot;Java City.&quot; Obviously, final arrays are not&lt;br /&gt;
immutable, and passing them around can violate encapsulation. No syntax in the Java programming language&lt;br /&gt;
provides a truly immutable array. This can lead to all kinds of inconsistencies. To avoid these problems, one&lt;br /&gt;
solution is to make the following changes:&lt;br /&gt;
1. Make the array private.&lt;br /&gt;
2. Add a getStates method.&lt;br /&gt;
3. Return a copy of the states array from the getStates method.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; OR&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
you can use Iterator as inner class to increase the performance of the program.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;Example:&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
&lt;b&gt;&lt;i&gt;public class ShippingInfo {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;private static final String[] states = {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&quot;AK&quot;, &quot;AZ&quot;, &quot;CA&quot;, &quot;DE&quot;, &quot;NV&quot;, &quot;NY&quot;};&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public static Iterator getStates() {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;return new StateIterator();&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public static class StateIterator implements Iterator {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;private int current = 0;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;/* from Iterator */&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public boolean hasNext() {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;return current &amp;lt; states.length;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;/* from Iterator */&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public Object next() {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;return nextState();&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;/* from Iterator */&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public void remove() {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;throw new UnsupportedOperationException();&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;/* custom typesafe next */&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;public String nextState() {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;if (current &amp;lt; states.length) {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;String state = states[current];&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;current++;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;return state;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;} else {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;throw new NoSuchElementException();&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;}&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
&lt;b&gt;&lt;i&gt;The following code snippet can be used to iterate through the array safely, without concern that it could be&amp;nbsp;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i&gt;accidentally damaged as read only.&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;Iterator iter = ShippingInfo.getStates();&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;while (iter.hasNext()) {&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;System.out.println(iter.next());&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/array-mutability.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-508832579881871182</guid><pubDate>Wed, 31 Jul 2013 02:53:00 +0000</pubDate><atom:updated>2013-08-01T05:18:10.096-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Under The Hood</category><title>Simple Object Immutability</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
While most objects are mutable, some are not. For example, any bean that provides a setXXX method is&lt;br /&gt;
mutable. Immutable objects can be used to define values or attributes that you don&#39;t want to be changed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;public class MyConstant {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;private double value;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public MyConstant(double value) {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;this.value = value;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public double getValue() {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;return value;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
This is not the only way to make object immutable, this is one of the ways available.&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/simple-object-immutability.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-1638007750049539368</guid><pubDate>Wed, 31 Jul 2013 02:27:00 +0000</pubDate><atom:updated>2013-08-01T05:18:14.886-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Under The Hood</category><title>Truth Behind String Literal</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Most of the problems with using String stem from the fact that String objects are immutable. Once they&#39;ve&lt;br /&gt;
been created, they cannot be changed. Operations that might appear to modify String objects actually generate completely new ones.&lt;br /&gt;
&lt;br /&gt;
Then what happen with string literal ?&lt;br /&gt;
The String and StringBuffer classes are meant to be used together to overcome this situation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;String xyz = &quot;x&quot; + y + &quot;z&quot;;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;It automatically transforms the code to&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;String xyz = new StringBuffer().append(&quot;x&quot;)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .append(y)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .append(&quot;z&quot;)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.toString();&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;This gives you an idea how String concatenation actually works. Note that two objects are created to perform&amp;nbsp;&lt;/b&gt;&lt;b&gt;the transformation: A new StringBuffer is created explicitly and a new String is returned from toString.&lt;/b&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/truth-behind-string-literal.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-269081474586774736.post-8227316981644494093</guid><pubDate>Wed, 31 Jul 2013 02:11:00 +0000</pubDate><atom:updated>2013-08-01T05:18:24.362-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Under The Hood</category><title>Do interface really extends the member of Object class?</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
To know this, we&#39;ll have to know about the members of &amp;nbsp;interface.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;
&lt;b&gt;Interface Members:&lt;/b&gt;&lt;/h2&gt;
The members of an interface are:&lt;br /&gt;
• Those members declared in the interface.&lt;br /&gt;
• Those members inherited from direct superinterfaces.&lt;br /&gt;
• If an interface has no direct superinterfaces, then the interface implicitly declares&lt;br /&gt;
a public abstract member method m with signature s, return type r, and throws&lt;br /&gt;
clause t corresponding to each public instance method m with signature s, return&lt;br /&gt;
type r, and throws clause t declared in &lt;b&gt;&lt;i&gt;Object&lt;/i&gt;&lt;/b&gt;, unless a method with the same&lt;br /&gt;
signature, same return type, and a compatible throws clause is explicitly declared&lt;br /&gt;
by the interface.&lt;br /&gt;
It is a compile-time error if the interface explicitly declares such a method m in&lt;br /&gt;
the case where m is declared to be &lt;b&gt;&lt;i&gt;final in Object&lt;/i&gt;&lt;/b&gt;.&lt;br /&gt;
It follows that is a compile-time error if the interface declares a method with a&lt;br /&gt;
signature that is override-equivalent to a public method of Object, but&lt;br /&gt;
has a different return type or incompatible throws clause.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://javadiscovery.blogspot.com/2013/07/do-interface-really-extends-member-of.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item></channel></rss>