Possible Duplicate:
What is the difference between “text” and new String(“text”) in Java?
Please explain the brief and detailed difference between following 2 statements:
String a= "somevalue";
String b = new String("somevalue");
I know that 2nd statement creates and provide memory to String Object b in heap. But why object a doesn’t get memory and its still allowed to operate on string methods.
aandbare references to Objects, not Objects.When you do
a = b;it doesn’t copy the Object, it copies a reference to an Object.A String has a
char[]inside it which is another object.agets an reference to an existing object so it may not need any extra memory.bget a reference to a newly created object so that requires more memory.This has nothing to do with how the object was created.