<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techtamasha.com &#187; Java</title>
	<atom:link href="http://www.techtamasha.com/category/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.techtamasha.com</link>
	<description>a new play everyday</description>
	<lastBuildDate>Sat, 22 May 2010 11:49:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>javac bug &#8211; no unique maximal instance</title>
		<link>http://www.techtamasha.com/javac-bug-no-unique-maximal-instance/97</link>
		<comments>http://www.techtamasha.com/javac-bug-no-unique-maximal-instance/97#comments</comments>
		<pubDate>Tue, 28 Apr 2009 12:08:13 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[javac bug]]></category>
		<category><![CDATA[no unique maximal instance]]></category>

		<guid isPermaLink="false">http://www.techtamasha.com/?p=97</guid>
		<description><![CDATA[While trying to compile my java class files with the 1.6 java compiler, I came across an error message that read : &#8220;no unique maximal instance exists for type variable U with upper bounds U&#8221; The weird thing was that the file compiled just fine in eclipse! On further probing I realized it&#8217;s a javac [...]]]></description>
			<content:encoded><![CDATA[<p>While trying to compile my java class files with the 1.6 java compiler, I came across an error message that read :</p>
<p>&#8220;<strong>no unique maximal instance exists for type variable U with upper bounds U</strong>&#8221;</p>
<p>The weird thing was that the file compiled just fine in eclipse!</p>
<p>On further probing I realized it&#8217;s a javac bug (<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954">check the bug here</a>)</p>
<p>Here&#8217;s the workaround -</p>
<p><span style="text-decoration: underline;">Bug Scenario</span> :</p>
<p>public class SomeObject {</p>
<p>&lt;U extends SomeObject&gt; U foo1() throws Exception {</p>
<p>SomeObject obj = new SomeObject();<br />
return obj.foo1();<br />
}</p>
<p>}</p>
<p><span style="text-decoration: underline;">Solve this way </span>:</p>
<p>public class SomeObject {</p>
<p>&lt;U extends SomeObject&gt; U foo1() throws Exception {</p>
<p>SomeObject obj = new SomeObject();<br />
return obj.<strong>&lt;U&gt;</strong>foo1();<br />
}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/javac-bug-no-unique-maximal-instance/97/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Method overriding and overloading in Java</title>
		<link>http://www.techtamasha.com/method-overriding-and-overloading-in-java/66</link>
		<comments>http://www.techtamasha.com/method-overriding-and-overloading-in-java/66#comments</comments>
		<pubDate>Fri, 04 Jul 2008 18:38:22 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[method overloading]]></category>
		<category><![CDATA[overriding]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=66</guid>
		<description><![CDATA[I know this may be trivial for many people but then it can be pretty confusing for new bees (pssst.. in the beginning me too used to be confused between overloading and overriding) OVERRIDING &#8211; when you extend a class and write a method in the derived class which is exactly similar to the one [...]]]></description>
			<content:encoded><![CDATA[<p>I know this may be trivial for many people but then it can be pretty confusing for new bees (pssst.. in the beginning me too used to be confused between overloading and overriding)</p>
<p><strong>OVERRIDING &#8211; when you extend a class and write a method in the derived class which is exactly similar to the one present in the base class, it is termed as overriding.</strong></p>
<blockquote><p><span style="color: #800000;">Example:</span></p>
<p><span style="color: #800000;">public class BaseClass{</span></p>
<p><span style="color: #800000;">public void methodToOverride()</span></p>
<p><span style="color: #800000;">{</span></p>
<p><span style="color: #800000;">//Some code here</span></p>
<p><span style="color: #800000;">}</span></p>
<p><span style="color: #800000;">}</span></p>
<p><span style="color: #800000;">public class DerivedClass extends BaseClass{</span></p>
<p><span style="color: #800000;">public void methodToOverride()</span></p>
<p><span style="color: #800000;">{</span></p>
<p><span style="color: #800000;">//Some new code here</span></p>
<p><span style="color: #800000;">}</span></p>
<p><span style="color: #800000;">}</span></p></blockquote>
<p>As you can see, in the class DerivedClass, we have overridden the method present in the BaseClass with a completely new piece of code in the DerivedClass.</p>
<p>What that effectively means is that if you create an object of DerivedClass and call the methodToOverride() method, the code in the derivedClass will be executed. If you hadn&#8217;t overridden the method in the DerivedClass then the method in the BaseClass would have been called.</p>
<p><strong>OVERLOADING -  when you have more than one method with the same name but different arguments, the methods are said to be overloaded.</strong></p>
<blockquote><p><span style="color: #800000;">Example:</span></p>
<p><span style="color: #800000;">public class OverLoadingExample{</span></p>
<p><span style="color: #800000;">public void add(int i, int j)</span></p>
<p><span style="color: #800000;">{</span></p>
<p><span style="color: #800000;">int k = i + j;</span></p>
<p><span style="color: #800000;">}</span></p>
<p><span style="color: #800000;">public void add(String s, String t)</span></p>
<p><span style="color: #800000;">{</span></p>
<p><span style="color: #800000;">int k = Integer.parseInt(s) + Integer.parseInt(t);</span></p>
<p><span style="color: #800000;">}</span></p>
<p><span style="color: #800000;">}</span></p></blockquote>
<p>As you can see in the example above, we have the same method add() taking two parameters but with different data types. Due to overloading we can now call the add method by either passing it a String or int <img src='http://www.techtamasha.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/method-overriding-and-overloading-in-java/66/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The &#8216;final&#8217; keyword in Java</title>
		<link>http://www.techtamasha.com/the-final-keyword-in-java/36</link>
		<comments>http://www.techtamasha.com/the-final-keyword-in-java/36#comments</comments>
		<pubDate>Mon, 03 Mar 2008 14:13:46 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[constant value for variable]]></category>
		<category><![CDATA[final]]></category>
		<category><![CDATA[final class]]></category>
		<category><![CDATA[final java]]></category>
		<category><![CDATA[final method]]></category>
		<category><![CDATA[final variable]]></category>
		<category><![CDATA[java final]]></category>
		<category><![CDATA[what is final in Java?]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=36</guid>
		<description><![CDATA[Time to explore the &#8216;final&#8216; keyword. When you use the keyword &#8216;final&#8217; with a variable declaration, the value stored inside that variable cannot be changed come what may!! So suppose you do this : final int i =10; After this, nowhere in your code can you change the value of &#8216;i&#8217; i.e. &#8216;i&#8217; will always [...]]]></description>
			<content:encoded><![CDATA[<p>Time to explore the &#8216;<strong>final</strong>&#8216; keyword.</p>
<p>When you use the keyword &#8216;final&#8217; with a variable declaration, the value stored inside that variable cannot be changed come what may!!</p>
<p>So suppose you do this :</p>
<p>final int i =10;</p>
<p>After this, nowhere in your code can you change the value of &#8216;i&#8217; i.e. &#8216;i&#8217; will always have a value &#8217;10&#8242; .</p>
<p>So i = 11;  //will give you a compile time error.</p>
<p>Now, suppose you did not initialize the final integer &#8216;i&#8217; while declaring it.</p>
<p>final int i;</p>
<p>Can you assign any value to this variable now?</p>
<p>Yes, you definitely can. But only once!</p>
<p>So,</p>
<p>final int i;</p>
<p>i = 10; //This is fine</p>
<p>i = 11; //Compiler error</p>
<p>Now that was the core stuff about &#8216;final&#8217; . Let&#8217;s learn a bit more in detail about it.</p>
<p>The &#8216;final&#8217; keyword can be used for a class declaration or method declaration.</p>
<p><strong>What happens when you use &#8216;final&#8217; for a class declaration?</strong></p>
<p><strong>A</strong>: You cannot have a sub class of a class declared final i.e. No other class can extend this class.</p>
<p><strong>What happens when you declare a method as final?</strong></p>
<p><strong>A</strong>: Simply put, the method cannot be overridden.</p>
<p><strong>Can you override a method(Not declared as &#8216;final&#8217;) present inside a final class?</strong></p>
<p><strong>A</strong>: Obviously Not.  Since there&#8217;s no way to extend a class declared final there&#8217;s no way of overriding its methods!</p>
<p><strong>What happens when I declare a method inside a class as both &#8216;private&#8217; and &#8216;final&#8217; ?</strong></p>
<p><strong>A</strong>: Making a class both &#8216;private&#8217; and &#8216;final&#8217; makes the &#8216;final&#8217; keyword redundant as a private method cannot be accessed in its sub class. But hey, you&#8217;ll be able to declare a method of the same name as in the base class if the method has been made private in the base class. But then that doesn&#8217;t mean you&#8217;re overriding the method. You&#8217;re simply declaring a new method in the sub class.</p>
<p><strong>Note:</strong> You cannot make an &#8216;abstract&#8217; class or method as &#8216;final&#8217; because an &#8216;abstract&#8217; class needs to be extended which will not be possible if you mark it as &#8216;final&#8217;!</p>
<p>I found this article on &#8216;final&#8217; worth reading:</p>
<p><a href="http://www.ibm.com/developerworks/java/library/j-jtp1029.html" title="http://www.ibm.com/developerworks/java/library/j-jtp1029.html" target="_blank">http://www.ibm.com/developerworks/java/library/j-jtp1029.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/the-final-keyword-in-java/36/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>New jdk 1.5 &#8216;for&#8217; loop ( Enhanced for loop-&#8217;for each&#8217;)</title>
		<link>http://www.techtamasha.com/new-jdk-15-for-loop-enhanced-for-loop-for-each/35</link>
		<comments>http://www.techtamasha.com/new-jdk-15-for-loop-enhanced-for-loop-for-each/35#comments</comments>
		<pubDate>Fri, 29 Feb 2008 13:16:05 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[1.5 for loop]]></category>
		<category><![CDATA[foreach loop]]></category>
		<category><![CDATA[java for each]]></category>
		<category><![CDATA[jdk 1.5 for loop]]></category>
		<category><![CDATA[new for loop]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=35</guid>
		<description><![CDATA[int a[] = {1,2,3,4,5}; for(int i =0;i&#60;a.length;i++) { System.out.println(&#8220;Value: &#8220;+a[i]); } This is so 1.4!!! Here&#8217;s how you do it with the new and enhanced for loop : for(int b: a) { System.out.println(&#8220;Value :&#8221;+b); } To make it easy for you to understand how to interpret the above condition, read it as follows: &#8221; for [...]]]></description>
			<content:encoded><![CDATA[<p>int a[] = {1,2,3,4,5};</p>
<p>for(int i =0;i&lt;a.length;i++)</p>
<p>{</p>
<p>System.out.println(&#8220;Value: &#8220;+a[i]);</p>
<p>}</p>
<p>This is so 1.4!!!</p>
<p>Here&#8217;s how you do it with the new and enhanced for loop :</p>
<p>for(int b: a)</p>
<p>{</p>
<p>System.out.println(&#8220;Value :&#8221;+b);</p>
<p>}</p>
<p>To make it easy for you to understand how to interpret the above condition, read it as follows:</p>
<p>&#8221; for each int b in a&#8230;..iterate&#8221;</p>
<p><strong>Basically what you have to do is &#8211; To the left of the colon in the for loop declare the datatype that is present inside the array and to the right of the colon write the name of the array.</strong></p>
<p>The above &#8216;for&#8217; loop will iterate as many times as there are elements in the array &#8216;a&#8217;. At every iteration int b will have the value of the next element.</p>
<p>In simple terms its like this:</p>
<p>for(int b: a) will internally expand like this : for(int i=0;i&lt;a.length;i++){int b=a[i];}</p>
<p>You can use this for loop for iterating over type collections too.</p>
<p>Example :</p>
<p>List&lt;MyObject&gt; al = new ArrayList();</p>
<p>//Create an Object of some class you want to put into the arraylist.</p>
<p>MyObject myObject = new MyObject();</p>
<p>MyObject myObject1 = new MyObject();</p>
<p>al.add(myObject);</p>
<p>al.add(myObject1);</p>
<p>for(MyObject o : al)</p>
<p>{</p>
<p>System.out.println(&#8220;Object hashcode is &#8220;+o.hashCode());</p>
<p>}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/new-jdk-15-for-loop-enhanced-for-loop-for-each/35/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple example to demonstrate that String object is immutable</title>
		<link>http://www.techtamasha.com/simple-example-to-demonstrate-that-string-object-is-immutable/31</link>
		<comments>http://www.techtamasha.com/simple-example-to-demonstrate-that-string-object-is-immutable/31#comments</comments>
		<pubDate>Tue, 19 Feb 2008 14:29:55 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=31</guid>
		<description><![CDATA[In my post &#8216;Difference between String and StringBuffer/StringBuilder&#8217; I told you stuff like String object is immutable( meaning the value stored in the object cannot be changed) and that when you perform operations such as concat or replace, internally a new object is created to hold the result. Below is a simple example that will [...]]]></description>
			<content:encoded><![CDATA[<p>In my post <a href="http://techtamasha.com/?p=28" target="_blank">&#8216;Difference between String and StringBuffer/StringBuilder&#8217;</a> I told you stuff like String object is immutable( meaning the value stored in the object cannot be changed) and that when you perform operations such as concat or replace, internally a new object is created to hold the result.</p>
<p>Below is a simple example that will make you believe that what I said about String object is indeed true!</p>
<p>String s = &#8220;Let&#8217;s test&#8221;;</p>
<p>s.concat(&#8221; if the String object is IMMUTABLE&#8221;);</p>
<p>System.out.println(s);</p>
<p>s = s.concat(&#8221; if the String object is IMMUTABLE&#8221;);</p>
<p>System.out.println(s);</p>
<p>The output of the above code will be:</p>
<p>Let&#8217;s test</p>
<p>Let&#8217;s test  if the String object is IMMUTABLE</p>
<p>That&#8217;s all people! The above piece of code proves that String is immutable and hence the results of operations like concat etc. should be stored into a new object.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/simple-example-to-demonstrate-that-string-object-is-immutable/31/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Here&#8217;s how you use StringBuilder</title>
		<link>http://www.techtamasha.com/heres-how-you-use-stringbuilder/30</link>
		<comments>http://www.techtamasha.com/heres-how-you-use-stringbuilder/30#comments</comments>
		<pubDate>Tue, 19 Feb 2008 09:46:21 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=30</guid>
		<description><![CDATA[Now get out of that bad habit of using String objects to perform operations on strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very append and other such operations more efficiently. Scene 1: typical coder&#8230;. using the same old String object to perform append operations. String s = &#8220;Hello&#8221;; s = s + [...]]]></description>
			<content:encoded><![CDATA[<p>Now get out of that bad habit of using String objects to perform operations on strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very append and other such operations more efficiently.</p>
<p>Scene 1: typical coder&#8230;. using the same old String object to perform append operations.</p>
<p>String s = &#8220;Hello&#8221;;</p>
<p>s = s + &#8221; World&#8221;;</p>
<p>system.out.println(s);</p>
<p>I agree it&#8217;ll give you &#8220;Hello World&#8221;, but fella&#8230;. let&#8217;s be different from the rest and think about making the code more efficient.</p>
<p>Enter Scene 2.</p>
<p>StringBuilder sb = new StringBuilder(&#8220;Hello&#8221;);</p>
<p>sb.append(&#8221; World&#8221;);</p>
<p>system.out.println(sb);</p>
<p>well&#8230; thats it!! You&#8217;ll get your &#8220;Hello World&#8221; but in a more efficient way <img src='http://www.techtamasha.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/heres-how-you-use-stringbuilder/30/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Difference between String and StringBuffer/StringBuilder in Java</title>
		<link>http://www.techtamasha.com/difference-between-string-and-stringbufferstringbuilder-in-java/28</link>
		<comments>http://www.techtamasha.com/difference-between-string-and-stringbufferstringbuilder-in-java/28#comments</comments>
		<pubDate>Fri, 15 Feb 2008 06:38:30 +0000</pubDate>
		<dc:creator>Nischal Shetty</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtamasha.com/?p=28</guid>
		<description><![CDATA[Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable. By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is &#8220;If String is immutable then how am I [...]]]></description>
			<content:encoded><![CDATA[<p>Well, the most important difference between String and StringBuffer/StringBuilder in java is that <strong>String object is immutable</strong> whereas <strong>StringBuffer/StringBuilder objects are mutable.</strong></p>
<p>By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is &#8220;If String is immutable then how am I able to change the contents of the object whenever I wish to?&#8221; . Well, to be precise it&#8217;s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.</p>
<p>So suppose you declare a String object:</p>
<p>String myString = &#8220;Hello&#8221;;</p>
<p>Next, you want to append &#8220;Guest&#8221; to the same String. What do you do?</p>
<p>myString = myString + &#8221; Guest&#8221;;</p>
<p>When you print the contents of myString the output will be &#8220;Hello Guest&#8221;. Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.</p>
<p><strong>Now isn&#8217;t that a performance issue?</strong></p>
<p>Yes, it definitely is.</p>
<p><strong>Then how do you make your string operations efficient?</strong></p>
<p>By using StringBuffer or StringBuilder.</p>
<p><strong>How would that help?</strong></p>
<p>Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as <strong>append</strong> would be more <strong>efficient</strong> if performed using <strong>StringBuffer/StringBuilder</strong> objects than String objects.</p>
<p><strong>Finally, whats the difference between StringBuffer and StringBuilder?</strong></p>
<p>StringBuffer and StringBuilder have the same methods with one difference and that&#8217;s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn&#8217;t thread safe).</p>
<p>So, if you aren&#8217;t going to use threading then use the <strong>StringBuilder</strong> class as it&#8217;ll be more <strong>efficient </strong>than <strong>StringBuffer</strong> due to the <strong>absence</strong> of <strong>synchronization</strong>.</p>
<p><a href="http://techtamasha.com/?p=30" target="_blank">Incase you do not know &#8211; Here&#8217;s how you use StringBuilder</a></p>
<p><a href="http://techtamasha.com/?p=31" target="_blank">A simple Example to demonstrate that String object is Immutable</a></p>
<p>Incase you still have any doubts regarding String or StringBuilder then do leave a comment. I&#8217;ll be more than eager to help you out.</p>
<p><strong>Note: StringBuilder was introduced in Java 1.5 (so if you happen to use versions 1.4 or below you&#8217;ll have to use StringBuffer) </strong></p>
<p><span style="color: #800000;">Link:  Have doubts about the &#8216;final&#8217; keyword? check out my post &#8211;  <a href="http://techtamasha.com/?p=36">Click Here!</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techtamasha.com/difference-between-string-and-stringbufferstringbuilder-in-java/28/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
