0

String str = new String("java");

Using string literal "java" in the constructor, an new string value is stored in string constant pool Using new operator a new string object is created in the heap with "java" as value

So here two objects are created, one is in string pool and another in heap.

Could you please help me to understand what is the usefulness of new in String?

Sudip
  • 92
  • 7
  • Using string literals helps save resources (memory). Nowadays though, this is of little practival value. – dsp_user Mar 16 '16 at 11:17
  • 2
    From javadoc : "Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable." – Arnaud Denoyelle Mar 16 '16 at 11:19
  • Could you please elaborate the ans. If we use the heap object still the string pool object was there and occupy the space. So could you please explain a more. Thanks in advance. – Sudip Mar 16 '16 at 14:08

0 Answers0