<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2767663100636558755</id><updated>2012-02-16T19:17:40.046-08:00</updated><category term='java basic programming'/><title type='text'>JavaTheHut.org - Tech Blog</title><subtitle type='html'>You don't have to be asleep to have a dream.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kentyang.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://kentyang.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Kent Yang</name><uri>http://www.blogger.com/profile/00388388657829835815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_77l0nkl6eMw/Sd2TBVKC21I/AAAAAAAAAGo/xlxqG_iA8Sg/S220/LegoKent.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2767663100636558755.post-5335971171702649899</id><published>2010-05-18T23:11:00.000-07:00</published><updated>2010-05-19T13:30:54.223-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java basic programming'/><title type='text'>Strange String Comparisons Behavior in Java and the Literal String Pool</title><content type='html'>&lt;style type="text/css"&gt;.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }.s0 { color: rgb(0,0,128); font-weight: bold; }.s1 { }.s2 { color: rgb(0,128,0); font-weight: bold; }.s3 { color: rgb(128,128,128); font-style: italic; }&lt;/style&gt;&lt;br /&gt;&lt;p&gt;Got the following question from a student in my &lt;a href="http://extension.ucsd.edu/studyarea/index.cfm?vAction=singleCourse&amp;vCourse=CSE-40028&amp;vStudyAreaID=14"&gt;Introduction to Programming class&lt;/a&gt;.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;I tried to compare two String variables with the == operator, and it wasn't working (have to use .equals()). I tried again right now, and it is working. I don't understand.&lt;/blockquote&gt;I suspect he ran into String Literal comparison quirk.   Here is an example to demonstrate the point.&lt;br /&gt;&lt;/p&gt;&lt;pre&gt;&lt;a name="l1"&gt;&lt;span class="ln"&gt;1    &lt;/span&gt;&lt;/a&gt;&lt;span class="s0"&gt;public class &lt;/span&gt;&lt;span class="s1"&gt;StringCompareTest &lt;br /&gt;&lt;a name="l2"&gt;&lt;span class="ln"&gt;2    &lt;/span&gt;&lt;/a&gt;{ &lt;br /&gt;&lt;a name="l3"&gt;&lt;span class="ln"&gt;3    &lt;/span&gt;&lt;/a&gt;   &lt;/span&gt;&lt;span class="s0"&gt;public static void &lt;/span&gt;&lt;span class="s1"&gt;main(String args[]) &lt;br /&gt;&lt;a name="l4"&gt;&lt;span class="ln"&gt;4    &lt;/span&gt;&lt;/a&gt;   { &lt;br /&gt;&lt;a name="l5"&gt;&lt;span class="ln"&gt;5    &lt;/span&gt;&lt;/a&gt;      String a = &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;; &lt;br /&gt;&lt;br /&gt;&lt;a name="l6"&gt;&lt;span class="ln"&gt;6    &lt;/span&gt;&lt;/a&gt;      String b = &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;; &lt;br /&gt;&lt;a name="l7"&gt;&lt;span class="ln"&gt;7    &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a name="l8"&gt;&lt;span class="ln"&gt;8    &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s3"&gt;// 1) Comparing Strings in the literal String pool&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l9"&gt;&lt;span class="ln"&gt;9    &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;if&lt;/span&gt;&lt;span class="s1"&gt;(a == b) &lt;br /&gt;&lt;a name="l10"&gt;&lt;span class="ln"&gt;10   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a equal b&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;br /&gt;&lt;a name="l11"&gt;&lt;span class="ln"&gt;11   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;else&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l12"&gt;&lt;span class="ln"&gt;12   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a does not equal b&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;a name="l13"&gt;&lt;span class="ln"&gt;13   &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a name="l14"&gt;&lt;span class="ln"&gt;14   &lt;/span&gt;&lt;/a&gt;      String c = &lt;/span&gt;&lt;span class="s0"&gt;new &lt;/span&gt;&lt;span class="s1"&gt;String(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;br /&gt;&lt;a name="l15"&gt;&lt;span class="ln"&gt;15   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s3"&gt;// 2) Comparing String from literal String pool&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l16"&gt;&lt;span class="ln"&gt;16   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s3"&gt;//    to a new String Object (not in the pool)&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l17"&gt;&lt;span class="ln"&gt;17   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;if&lt;/span&gt;&lt;span class="s1"&gt;(a == c) &lt;br /&gt;&lt;a name="l18"&gt;&lt;span class="ln"&gt;18   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a equal c&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;br /&gt;&lt;a name="l19"&gt;&lt;span class="ln"&gt;19   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;else&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l20"&gt;&lt;span class="ln"&gt;20   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a does not equal c&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;a name="l21"&gt;&lt;span class="ln"&gt;21   &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a name="l22"&gt;&lt;span class="ln"&gt;22   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s3"&gt;// 3) Finally, what you should always use when&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l23"&gt;&lt;span class="ln"&gt;23   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s3"&gt;//    comparing Objects, the equals method&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;br /&gt;&lt;a name="l24"&gt;&lt;span class="ln"&gt;24   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;if&lt;/span&gt;&lt;span class="s1"&gt;(a.equals(c)) &lt;br /&gt;&lt;a name="l25"&gt;&lt;span class="ln"&gt;25   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a equal c&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;a name="l26"&gt;&lt;span class="ln"&gt;26   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0"&gt;else&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;a name="l27"&gt;&lt;span class="ln"&gt;27   &lt;/span&gt;&lt;/a&gt;         System.out.println(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;a does not equal c&amp;quot;&lt;/span&gt;&lt;span class="s1"&gt;); &lt;br /&gt;&lt;br /&gt;&lt;a name="l28"&gt;&lt;span class="ln"&gt;28   &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a name="l29"&gt;&lt;span class="ln"&gt;29   &lt;/span&gt;&lt;/a&gt;   } &lt;br /&gt;&lt;a name="l30"&gt;&lt;span class="ln"&gt;30   &lt;/span&gt;&lt;/a&gt;} &lt;br /&gt;&lt;a name="l31"&gt;&lt;span class="ln"&gt;31   &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;In Java, String literals go into a memory pool call the Literal String Pool.  The reason this is done is to save memory.  If two different Strings are initialize to the same literal value (“123”).  Java assigns the same reference to a String Object from the literal String Pool.   In the above example, both a and b are assigned the same literal value of “123”.  Rather than creating two String literal Objects, Java creates one and puts it in the literal String pool.  Anytime it is used again, it can be reused from the literal String Pool.    This can be misleading to new Java developers because the standard procedure is to tell students to use the equals() method to compare Objects for equality and only use == for primitives.  In the case where the literal String already exists in the literal String pool, using == (shallow comparison) works because new Strings being assigned the same literal value will simply get the reference to a String from the literal String pool.&lt;/p&gt;&lt;p&gt;To further illustrate the difference, if a new String Object is created using the new operator and initialized in the constructor to the String “123”, then technically, both Strings a and c are equal.  However, when we use == for the comparison, String a does not equal c.  String c was initialized in the constructor to the literal value “123”.  However, because String c was explicitly created using the new operator and therefore not part of the literal String pool, String comparison using == (shallow comparison) does not work.  &lt;/p&gt;&lt;p&gt;In the end, step 3, we return to doing Object comparisons the right way.  The last test use the equals() method to perform a deep comparison of the two String Objects (a and c).  The output confirms that both Strings are equal.  &lt;em&gt;Moral of the story, stick with equals method for comparing Objects.&lt;/em&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2767663100636558755-5335971171702649899?l=kentyang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kentyang.blogspot.com/feeds/5335971171702649899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2767663100636558755&amp;postID=5335971171702649899' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/5335971171702649899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/5335971171702649899'/><link rel='alternate' type='text/html' href='http://kentyang.blogspot.com/2010/05/strange-string-comparisons-behavior-and.html' title='Strange String Comparisons Behavior in Java and the Literal String Pool'/><author><name>Kent Yang</name><uri>http://www.blogger.com/profile/00388388657829835815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_77l0nkl6eMw/Sd2TBVKC21I/AAAAAAAAAGo/xlxqG_iA8Sg/S220/LegoKent.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2767663100636558755.post-4205259099792792429</id><published>2009-01-26T22:05:00.000-08:00</published><updated>2009-01-26T22:29:09.010-08:00</updated><title type='text'>Autoboxing support C# VB (.NET) vs Java</title><content type='html'>I am currently doing .NET by day but still teaching Java for UCSD by night.   During the last lecture, autoboxing came up.  So I thought I would do a simple comparison between .NET and Java on how it handles Autoboxing.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// AutoboxTest.cs&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;class AutoboxTest&lt;br /&gt;{&lt;br /&gt; static void Main()&lt;br /&gt; {&lt;br /&gt;    Console.WriteLine(22.ToString());&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And like wise in VB&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;''' AutoboxTest.vb&lt;br /&gt;Module AutoboxTest&lt;br /&gt; Sub Main()&lt;br /&gt;    Console.WriteLine(22.toString())&lt;br /&gt; End sub&lt;br /&gt;End Module&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So I thought I would give it a try in Java:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class AutoboxTest&lt;br /&gt;{&lt;br /&gt; public static void main(String args[])&lt;br /&gt; {&lt;br /&gt;    System.out.println(((Integer)22).toString());&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note: I could not get the simple form "22.toString()" to work unless I cast the literal to an Integer first.  Score one for .NET.&lt;br /&gt;&lt;br /&gt;No real numbers were harmed in this example.  The number 22 was chosen because that is my hockey jersey number and paying tribute to the classic movie Casablanca.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2767663100636558755-4205259099792792429?l=kentyang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kentyang.blogspot.com/feeds/4205259099792792429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2767663100636558755&amp;postID=4205259099792792429' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/4205259099792792429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/4205259099792792429'/><link rel='alternate' type='text/html' href='http://kentyang.blogspot.com/2009/01/autoboxing-support-c-vb-net-vs-java.html' title='Autoboxing support C# VB (.NET) vs Java'/><author><name>Kent Yang</name><uri>http://www.blogger.com/profile/00388388657829835815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_77l0nkl6eMw/Sd2TBVKC21I/AAAAAAAAAGo/xlxqG_iA8Sg/S220/LegoKent.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2767663100636558755.post-532678509354575363</id><published>2008-12-03T19:39:00.000-08:00</published><updated>2008-12-03T19:51:12.914-08:00</updated><title type='text'>First run with IPhone and RunKeeper</title><content type='html'>I love my IPhone.  Best phone, hands down.  I downloaded RunKeeper App last night.  My first test run today.  This app is frakkin awesome.  I've found my new inspiration and motivation to stay in shape.  The ROI on my IPhone is already beyond my expectations.  Sample image below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_77l0nkl6eMw/STdTlwny0SI/AAAAAAAAAFs/H7L-SJ7_X3A/s1600-h/FirstRun20081203.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 215px;" src="http://3.bp.blogspot.com/_77l0nkl6eMw/STdTlwny0SI/AAAAAAAAAFs/H7L-SJ7_X3A/s320/FirstRun20081203.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5275777396659900706" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2767663100636558755-532678509354575363?l=kentyang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kentyang.blogspot.com/feeds/532678509354575363/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2767663100636558755&amp;postID=532678509354575363' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/532678509354575363'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/532678509354575363'/><link rel='alternate' type='text/html' href='http://kentyang.blogspot.com/2008/12/first-run-with-iphone-and-runkeeper.html' title='First run with IPhone and RunKeeper'/><author><name>Kent Yang</name><uri>http://www.blogger.com/profile/00388388657829835815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_77l0nkl6eMw/Sd2TBVKC21I/AAAAAAAAAGo/xlxqG_iA8Sg/S220/LegoKent.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_77l0nkl6eMw/STdTlwny0SI/AAAAAAAAAFs/H7L-SJ7_X3A/s72-c/FirstRun20081203.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2767663100636558755.post-6508341153364055311</id><published>2008-11-07T21:43:00.000-08:00</published><updated>2008-11-07T21:59:59.382-08:00</updated><title type='text'>Time to blog again...</title><content type='html'>&lt;span style="font-family: arial;"&gt;It's time to start blogging again.  This post from &lt;a href="http://www.tbray.org/ongoing/When/200x/2008/10/27/Contribute"&gt;&lt;span style="font-family: arial;"&gt;Tim Bray&lt;/span&gt;&lt;/a&gt; was the motivation (kick in the butt) I needed.  In addition, I have been using &lt;a href="http://twitter.com/kentyang"&gt;Twitter &lt;/a&gt;quite a bit lately and having lots of fun so the timing is right.  The only difference is this time around, I am using blogger rather than roller.  Blogging has come a long way and it looks like Blogger has plenty of toys to keep me busy.  I'll use this blog as a way to share what I learn, what I see (pictures), and what I think is cool.  Stay tuned... &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2767663100636558755-6508341153364055311?l=kentyang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kentyang.blogspot.com/feeds/6508341153364055311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2767663100636558755&amp;postID=6508341153364055311' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/6508341153364055311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2767663100636558755/posts/default/6508341153364055311'/><link rel='alternate' type='text/html' href='http://kentyang.blogspot.com/2008/11/time-to-blog-again.html' title='Time to blog again...'/><author><name>Kent Yang</name><uri>http://www.blogger.com/profile/00388388657829835815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_77l0nkl6eMw/Sd2TBVKC21I/AAAAAAAAAGo/xlxqG_iA8Sg/S220/LegoKent.JPG'/></author><thr:total>0</thr:total></entry></feed>
