Interview Question
Software Engineer / DevelopersWhy are we talking about Garbage collection when we know about string literal pool. This example is a mere test of immutability of strings and how string created on heap using string literal or new operator.
Closely looking at above example Creating String by using String Literal or New approach looks the same. In both the cases String object gets created on the heap. We need to check out for 'a+b', from my experience and theory one more string object gets created and that will be printed on to IO by system.out statement.
So no difference except the fact that.. if String literal lookup approach in first case is costlier if lookup doesn't find any existing string. In other case creating string objects using new operator directly creates the object on the heap without even bother to perform string lookup. So time wise second is much faster than first one for given example.
'badalrocks' comments are fine for third string with same string literal. Question is which one is more efficient for given example. ( Efficiency = Both Time and Memory ) . So for me second is more efficient.
In 1st case... it will lookup in the heap for the string objects, it they exist variables will point to them... if they do not exist then they will be created....so if they do not pre-exist then I guess the creation time will be same as using "new String"... however in first case we had some lookup time....however if they pre-existed then it would be faster in first case. so I guess MasterSolution is right.
I think first way will be more efficient. As compiler will optimize sysout as follows
System.out.println("abhaydeol"); // It will also add a+b to the string pool.
comments?
The explicit String constructor has the same result as using double quotes to implicitly create a string reference, in both cases the given string is assigned to the reference variable. However, passing a double quoted string argument to the String constructor actually makes two strings; the argument string is implicitly created inline and then its contents are copied to the new String instance. For this reason this constructor is largely redundant and not recommended for general purposes. The String constructor with String argument is rarely used except to create an independent copy of an existing string variable.
Adding to what mostUnlucky said..
String a =new string("tmp"); 2 instance..one in heap,one in string pool.
String b=new String("tmp");.. only 1 instance..one in heap,able to find string literal "tmp" in String pool thats why wont create one more instance.
String a="tmp" one instance..only in heap.
String b="tmp" one instance..
String a=new String("abhay");
String b=new String ("deol");
System.out.println(a+b);
in the above example, these two strings can be garbage collected.
but String a="abhay", String b="deol" cannot.
This is a classic example of immutability of String objects. I think the first 3 lines are more efficient. Because if say one more String object is created in such a manner:
- badalrocks January 29, 2009String z="deol";
Then the heap will not contain one more string object "deol". Since "deol" is already referenced by 'b', both 'b' and 'z' will refer to one String object on the heap.
Whereas if we use the new keyword:
String z=new String("deol");
Then one more object will be created on the heap and 'z' and 'b' will refer to two different objects that contain same data.
Check out the following URL for more information:
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1