On Thursday, October 8th 2009, I was teaching basic programming lab for the 1st semester students in Swiss German University and the topic to be covered are from JAVA Concepts book chapter 1-4. When, we were discussing about the String class, a student named Viktor Wijaya asked me :
Viktor: “I was wondering why we can do String text = “some text”? Don’t we have to instantiate every object by using a new keyword like MyClass a = new MyClass()”.
To be honest, I didn’t expect that question at all and I realized that after years of using JAVA that String class doesn’t have to be instantiated by a new keyword. I got to say that’s an excellent question and I got no answer to that in the meantime. My curiosity went through and I searched for the answer of the question launched by Viktor. After a while searching in the internet, I found the answer to the 1 million dollars question. So Viktor, here’s the answer to my understanding:
String is a rare class in Java. It’s allowed to straddle the line between behaving as an object (using new() ) and behaving as a primitive (using direct assignment). The designers knew String objects would be prominent in almost any program. To conserve the run-time cost of creating them, they chose to allow static String assignments. Static assignments are “paid for” at compilation time, where they can be less of a burden.
Another difference on the declaration is on the object creation. String class is an immutable class, so once you declare it, it can’t be changed. You might think it changed but actually the system creates a new object and move the pointer of the variable to the new object.
Now, about the instantiation. If you use the new() keyword, the system will create a new object every time the new() keyword is invoked. So if you declare a String as String text = new String(data), where data is an array of characters, the system will create a new object when the instantiation is invoked. Because of this behaviour, instantiating two similar string over two different variables will yield different objects. Or in other words:
char[] data = {‘a’,'b’,'c’};
String text1 = new String(data);
String text2 = new String(data);
text1 == text2 ? ==> will yield a false result since the objects are different.
However, using direct assignment for the instantiation of String will yield a different process. When we do, String text = “a text”, it will create a new object to be pointed to the “text” reference. When we instantiated another string variable using direct assignement, the system will search for the specified string that has been instantiated previously. If it found a matching string, the system will use that object. If not, it will create a new object. Or in other words:
String text1 = “abc”;
String text2 = “abc”;
text1 == text2? ==> will yield a true result since “abc” has been instantiated in “text1″ variable.
So, there you go Victor. The answer to why a String class can be instantiated using direct assignment. I hope you understand the explanation I gave to you. If not, we’ll discuss it in the class. Cheers!
Recent Comments