Archive

Archive for the ‘programming’ Category

Picked up some new books

September 1st, 2008

Last week I finished the book “Prelude to Foundations” by Isaac Asimov.  It was recommended to me by my boss, and I have to say it was one of the better books I have read in a while.  I usually try to switch up reading technical books with non-technical books, and since I haven’t read any new technical books I decided to head over to chapters and pickup some new books.

I have head great things about both Java Concurrency by Brian Goetz, and Effective Java 2nd Edition by Joshua Bloch.  The real problem with both these books is they are hard to find in store.  I happened to come across both of them at my local Chapters, and quickly snatched them up.

I have been reading Java Concurrency over the past couple of days and I have to say this is one of the best Java books I’ve read in a while.  Brian Goetz has a way of writing that really helps bring complex problems into perspective.

One of my initial gripes about this book was the lack of exercises at the end of the Chapters, however now that I’m a little ways into the book I’ve noticed that the examples chosen for each Chapter progress almost as a set of exercises in themselves.  Not to mention that Brian picks examples that relate well to the real world, instead of most of the silly easy examples found in other books I’ve read.

I would definitely recommend this book to anyone looking to delve into the world of Java Concurrency, or even someone looking to freshen up on the latest stuff available in JDK 1.5 in terms of threading.  This book has really helped me in my pet projects and in my day-to-day work so I would definitely recommend getting it and keeping it close by.

I’ve yet to really get into Effective Java, but the little bits I’ve read here and there have really shown me that this this book lives up to all the hype about it.  Joshua is a smart man, and this book definitely reflects his intelligence.  It is well organized and shows you things about Java that you never thought even existed.  I’m really excited to delve deaper into this one.

I’ll write a more thorough review once I’m finished with it.  Anyway until next time …

kungfuice Computers, General, Technology, java, programming , , ,

Why Linked Lists Are Incompatible With Parallel Programming

December 21st, 2007

Arch Robison (of Intel) writes an interesting piece on his blog about why Linked Lists are incompatible with Parallel Programming, and even gives some reasons as to why to avoid them in your code.

Quite an interesting article, I’ll definitely be looking at some of my code that includes linked lists and looking for some types of alternatives.

Link to Article

kungfuice programming

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