Monday 3 March 2014

Java Immutable object and practical approach to learn Java

Let start with Java immutable object first. I will share my story on Immutable Object and thoughts on practical approach to learn Java.

Java Immutable Object

This is one of the first thing that you will know if you choose to learn Java. Immutable object is an object that cannot be changed once created in Java. A sample immutable class look like this:

public final class ImmutableObject {
 
 private final String value;

 public String getValue() {
  return value;
 }

 public ImmutableObject(String value) {
  this.value = value;
 }

}

There is nothing special about the above class except that once an object is created, it is read-only. There are a lot of Java classes that are immutable; for example String class, all of the primitive wrapper classes like Integer, Byte, Float, Double,...

How did I learn about Immutable Object?

Immutable Object is not taught in school. I only noticed it when I learn how StringBuffer is used to concatenate String. Here is one example:

public static String ugly(String foo, String bar) {
     StringBuffer buffer = new StringBuffer();
     buffer.append (foo);
     buffer.append (" ");
     buffer.append (bar);
     return buffer.toString();
}

This example is very simple and there is nothing fancy of technical aspect here. However, I want to share about my personal experience when I learnt this stuff. I kept wondering why they need to create StringBuffer, why don't simply do it like

public static String ugly(String foo, String bar) {
     return foo.append(" ").append(bar);
}

It will be much shorter and more convenient to work with String this way. The answer lies in Immutable Class. Because String is immutable, it cannot be changed. That why they include StringBuffer as a convenient way for developers to work with String. Then I started to question why we need to create Immutable class. After thinking about the rationale behind that decision for a while, I came to conclusion that they want to protect the parameter objects that being passed to method.

Here is example:

     String foo = "foo"
     String bar = "bar"
     String combo = ugly(foo, bar);

If the Java creator allow me to change String object like I wish, I may accidentally modify the value of the String "foo". Somehow, Java creators do not want that to happen. Curious about the found out, I verified this thought by asking my friend, Google. I only figured out one of the 5 reasons why Java have immutable object.

1. First and foremost, String is used in many critical functions. One example is class loading mechanism. If String is immutable, hacker can alter class loading mechanism on JDK to load the unwanted class. This msy hamper the security of system. Java was designed with security as the most important goal and they cannot let that happen.

2. Some data structure in Java like Hashtable or HashMap can use object as key, they will not want the key to be modifiable.

3. Immutable is thread safe. As they cannot be changed, developers do not need to worry about thread safe issue.

4. They can cached hashed code for immutable object because it is assured that object will never be changed.

5. String pool. This is built-in mechanism of JVM to save memory when creating String object. If you declare

String a = "someText";
String b = "someText";

there will be only one object created in JVM and two pointers are pointing to the same object. They can not cache it this way if they are not sure that the object is immutable.

So, what can we learn from this?

After this story, we can have some conclusions here

1. My Java in the beginning day is really suck. If you start to learn Java, you will need to go through this stage too.

2. There is no evidence that the Java creators did not think thoroughly when they created data structures and we can improve our knowledge by learning how they did that.

3. Questioning about things and trying to figure out rationale behind is fun.

I would suggest all of learners of Java to be more pro-active in their pursue of knowledge. When you start questioning about why things are created that way and taking initiative to find out yourself, you will pick up knowledge pretty fast. It doesn't matter whether you are right or wrong, as long as you try to figure out an answer yourself, you will understand concepts faster.

Through many years of observation, I conclude that developer take responsibility for seeking knowledge themselves will end up more knowledgeable and confident in work.

http://www.myrkothum.com/the-meaning-of-the-finger-pointing-to-the-moon/

May be most readers already familiar with the story of Finger pointing to the Moon. You will need to find the moon yourself, I can only offer a finger in this blog (actually, I used all 10 fingers to type this post).



1 comment: