Please use the search next time because then you would have found this
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
May or may not create a new String object.
If a String object with the literal "abc" already exists the reference 'a' will only point to it.
Since String objects are immutable.
Where as,
String a = new String("abc");
will always create a new Sting object.
Thanks Wouter, I came to know the exact difference between these two type of instantiation..
So, String a="abc";
its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...
Constant literal table can be cleared only if JVM is terminated...
Ram Narayan.M wrote:its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...
Umm, no. The second format uses the literal too, so how do you figure it's less "expensive"?
class A {
public static void main(String... args) {
String str="java";
String str1 = new String("java");
System.out.println(str.equals(str1));
System.out.println(str.hashCode()+"---"+str1.hashCode());
}
}
When i tested with this program, i got the output as
true
3254818---3254818
Two String object is "equal" and their hashcodes are also equal...
There is a fact .. if two objects are equal by equal() method, then their hashcodes are also equal... Implicitly its the only one object referred by the two reference variables... So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?
Ram Narayan.M wrote:So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?
To check the reference equality you should have used '==', and obviously it is going to return false as you have used the 'new' keyword.
So, both are not the same references.
Ram Narayan.M wrote:So, String a="abc";
its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one....
is a compile time constant and it will be loaded into the object heap as well as referenced from the string literal pool, but the second one will store the string in the object heap again - it wont take the already available reference in the pool,just because you have used 'new'.
So, see for yourself which one seems to be more efficient in terms of memory.
What if there are large number of string literals referred by the String Litreal Pool?... It stays for long time and leads to the heap
memory space unavalability. So when the JVM is terminated, the string literal pool entries will be cleared...
If its String a= new String("abc");
Ok.. each time "abc" string object is created in the heap due to "new" keyword...
When the reference to the "abc" object is not there, it will be marked for removal by GC... But not in the case of compile time String literals..
Ah...4 days have passed by! Just forgot this thread. Did some deep googling on Strings.. Now,i'm pretty much clear. Hope this helps you too.
Ram,
See if this is clear.
Anything within a double quotes is a String literal.
would actually create 2 objects & return 1 reference back. Since "hello" is a String literal,the compiler will make a note of it at compile time and will create a String object in the object heap & the reference is stored in the String literal pool when the Class is loaded for the very first time. This is followed by a creation of another new String object with exactly the same content "hello"(because of new String()). The reference of this second newly created String object in the heap is returned back to str.
If the String str goes out of scope or assigned null, it will be garbage collected sometime or the other.
The problem being, the first reference(of the literal) will continue to exist as long as the JVM runs. It will never be unreferenced - the literal pool holds the reference,remember?
So, again I leave it to you to decide which of the 2 is more efficient.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: What is the difference in String a = "abc" and String a = new String("abc")