Archive

Archive for November, 2007

Java String Comparisons

November 14th, 2007

Today I thought I would take some time to outline basic String comparison in Java. To many newcomers using Java doing comparisons using String can be confusing. Take for example the following piece of code which compares two String literals and two String objects. To newcomers of the Java language the following is confusing. Let’s take some time to outline how this actually works, and what actually makes it confusing.

<font color="#808080">01</font> <font color="#7f0055"><strong>public class </strong></font><font color="#999999">Test </font><font color="#999999">{</font>
<font color="#808080">02</font>     <font color="#7f0055"><strong>public static </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#999999">main</font><font color="#999999">(</font><font color="#999999">String</font><font color="#999999">[] </font><font color="#999999">args</font><font color="#999999">) {</font>
<font color="#808080">03</font>         <font color="#999999">String a = </font><font color="#2a00ff">"abc"</font><font color="#999999">;</font>
<font color="#808080">04</font>         <font color="#999999">String b = </font><font color="#2a00ff">"abc"</font><font color="#999999">;</font>
<font color="#808080">05</font>         <font color="#999999">String c = </font><font color="#7f0055"><strong>new </strong></font><font color="#999999">String</font><font color="#999999">(</font><font color="#2a00ff">"abc"</font><font color="#999999">)</font><font color="#999999">;</font>
<font color="#808080">06</font>         <font color="#999999">String d = </font><font color="#7f0055"><strong>new </strong></font><font color="#999999">String</font><font color="#999999">(</font><font color="#2a00ff">"abc"</font><font color="#999999">)</font><font color="#999999">;</font>
<font color="#808080">07</font>         <font color="#999999">System.out.println</font><font color="#999999">((</font><font color="#999999">a==b</font><font color="#999999">)</font><font color="#999999">;</font><font color="#3f7f5f">//true</font>
<font color="#808080">08</font>         <font color="#999999">System.out.println</font><font color="#999999">((</font><font color="#999999">c==d</font><font color="#999999">))</font><font color="#999999">;</font><font color="#3f7f5f">//false</font>
<font color="#808080">09</font>     <font color="#999999">}</font>
<font color="#808080">10</font> <font color="#999999">}</font></code>

<code><font color="#999999">


First let’s look at the instructions used to generate a String literal in Java. It’s fairly straight forward for anyone who’s seen assembly before. We simply store the literal value using the instruction sets store command.

Instruction used to generate String literal
0: ldc #2; //String abc
2: astore_1

On the other hand there are many more instructions used to generate a String using the new operator. Since we are using the new operator we create a new reference to memory location, duplicate it, load the String literal and invoke a special init function on that new memory. In Java terms we are merely creating a new object and invoking that objects constructor with the parameter “abc”.

Instruction used to generate new String()
6: new #3; //class java/lang/String
9: dup
10: ldc #2; //String abc
12: invokespecial #4; //Method java/lang/String."<init>":(Ljava/lang/String;)V
15: astore_3

So the question still remains, why does a==b return true, and c==d return false? First let’s look at the == operator itself. The == operator works on String object references. If two String references point to the same object in memory, the comparison returns a true result. Otherwise, the comparison returns false, regardless of whether the text has the same character values.

The == operator does not compare actual char data. So we know now that the == operator is looking at comparing two object references, but how can a==b be true?

Well this comes down to the different way Java handles literals. The Java platform creates an internal pool for string literals and constants. String literals and constants that have the exact same char values and length will exist exactly once in the pool.

Subsequent comparisons of String literals and constants with the same char values will always be equal. So you see we could create a million String literals with the value “abc” but Java would only have reference in it’s internal pool to a String literal with value “abc” and length 3.

On the other hand c and d are not String literals they are String objects, and therefore are not created in the Java String literal and constant pool. These objects will each have it’s own reference in memory and therefore will have different values for their references.

Using the == operator to check whether their reference points to the same value will never be true since each occupies it’s own slot in memory.

If we wanted to compare the actual values stored within the String instead of their references, we would simply use the equals method or the compareTo method.

I hope this helps clarify some of the mystery behind how Java handles String and I hope I have helped increase your understanding of String comparisons in Java. Next time we’ll take a closer look at Natural Langauge text comparison in the Java language.

kungfuice Computers, Technology, java, programming

Ubuntu Gutsy

November 1st, 2007

I have been running Ubuntu Gutsy for the last couple of months while it was in Beta and RC stages, but this week marked Gutsy’s full release to the public. I have to say that this is probably one of the best Linux Distro’s I have used to date in terms on out of the box functionality and completeness. It looks like finally someone has listened to many people cries to make Linux more available to regular users, in order to penetrate the Desktop software market.

Although I wouldn’t say Gutsy is a complete success, it definitely has hit the nail on the head in terms on where Linux distributions need to move in the coming future. Being a software developer Linux has always felt comfortable to me. I never really took the time to consider that not everyone in this world wants to spend a day tweaking the kernel, or trying to configure some bleeding edge application. At the same time to me this was one of the things I always loved about Linux, was that it afforded me these luxuries. To me Ubuntu Gutsy finds a happy medium between both of these extremes.

Out of the box Gutsy can be used by any regular user without the need to spend time configuring and setting up the user environment. However Gutsy does not prevent more experienced users from taking this approach. Installing a few tools you can build your own kernel image or compile any bleeding edge software you need. To me this is the real strength in Ubuntu and especially in Gutsy. I find that in my day to day life I don’t always have to be using the the bleeding edge, or do I feel the need to spend hours on end trying to get some new software configured. I am happy with just turning my computer on, firing up my development environment and getting to real work. Don’t get me wrong I still enjoy “playing” around with Linux, but I have found more and more that I expect an OS that just kind of works without too much fuss. I would rather use my time to do my job, or write code then fight to get things working.

I think Gutsy does a good job of achieving this goal for me, since it includes some of the latest bleeding edge software like compiz-fusion, gnome 2.20, open-office 2.3 etc … but it doesn’t require that I spend hours on end getting any of this working, because all of it just works from the start. For example in Feisty I was running beryl, and just before I upgraded I had made the switch to compiz-fusion. Both these pieces of software took me many hours to get configured properly, not to mention that every couple of days they would break because of some new change to the SVN code base. This was acceptable for me since I knew ahead of time that both pieces of software were bleeding edge and would have their issues, but when you need to print a simple PDF and can’t get X to come up it can be a little frustrating.

Gutsy has done a good job of integrating these new bleeding edge pieces of software but at the same time running versions that are relatively stable and offer a good user experience.

A supposed great new feature of Gutsy is it’s automatic printer configuration. Gutsy will find printers connected to your computer and on your Network and automatically configure them to “work”. Well I have yet to get this feature to “work” properly, and since my printer worked just fine in Feisty I find this to be extremely frustrating. Although I don’t physically have a printer connected to my machine I do have a laser printer shared from a Windows XP box in our basement. It’s a simple Samba based printer share that worked flawlessly in Feisty and on all my other machines, except for Gutsy. It simply won’t print anything, it won’t even spit out garbage, since I’ve been so busy with school as of late I haven’t really gotten a chance to sit down and figure out why it’s not working anymore.

Other then this oddity though, Gutsy has been a great upgrade for me, and I don’t regret moving up to this release. I recommend Gutsy for anyone who wants to use Linux, whether you’ve been using it for years or simply want to make the plunge to the Linux dark side. I promise you, you won’t be disappointed.

Look forward to some configuration and tweaking articles I’m putting together, to really maximize how your gutsy install performs.

kungfuice Technology, Ubuntu