-4

There are two ways to create string in java as below:

  1. String s = new String("Hellow");
  2. String s1 = "Hellow";

In case 1, there are two objects(one in String constant pool and another one in Heap memory) which will be created while in case 2, only one object will be created in String constant pool only.

As per my understanding, whenever we are creating object using new keyword one object is also getting created in string constant pool. So why to create a string using new keyword, instead we should always create via string literal.

So could you guys please let me know if my understanding is right or wrong. And if it is wrong, please provide the scenario where it is must to create the string using "new".

Rohit
  • 406
  • 1
  • 5
  • 21
  • 2
    Side note: Spelling of "Hello" is wrong. We call it a typo. – Aniket Sahrawat Mar 21 '20 at 12:31
  • "So why to create a string using new keyword, instead we should always create via string literal." - please explain to me why you would use `new String`. – f1sh Mar 21 '20 at 12:34
  • "*As per my understanding, whenever we are creating object using new keyword one object is also getting created in string constant pool*" not quite, you can always call `new String()` and create string equivalent to `""` (no text) which will not be placed in pool. Also `new String(new char[]{'h', 'e', 'l', 'l', 'o'})` will create String representing "hello" which will not be placed in pool. In your case `new String("Hellow");` `new String(...)` uses string *from* pool created via `"Hellow"` string literal, but string created via `new` will not be placed in pool. – Pshemo Mar 21 '20 at 13:34

3 Answers3

0

Best way is option 2. While you can create string using new keyword. What will happen in that case is the literal"hellow" will be saved into constant pool maintained by String class. Also an obj will be created into heap. Check this: A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

If you are considering about memory upon creation of objects I would suggest to trust jvm let it garbage collect the objects which do not have references.

When you do String S1 = "something" ; or String S1 = new ("something"); you are creating an object of litteral not present in pool. Further when you change the value of S1 as S1="yes again assigned" here you are creating a new object. And leaving the literal "something" into the pool until garbage collected.

Better approach is to use StringBuffer class.

0
String s = "Hellow";

This is 99% of all cases when you have to create a String. It creates new String and put it into String Pool or reuse existed String already existed in the String Pool.

String s = new String("Hellow");

This approach is useful when you really need to have exactly different String object that will not be stored (and shared) in String Pool. You're right, that here you have exactly two objects: "Hellow" - this is string literal and stored in String Pool and new String() that will be store in the Heap.

I could give you example. When you have to create a new string from byte array, you have to use new String(byte[] buf).

Buy the way. To move string to String Pool you can use String.intern():

String one = "one";
String two = new String("one");   // one != two, one.equals(two) = true
String three = two.intern("one"); // one == three, one.equals(three) = true
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

1=>. String s=new String(“ABC”); 2=>. String s1=“ABC”; If u create both 1 and 2 create only one “ABC” object on string pool reference to heap to both . Because of 1 and 2 characters are same . Why do you use object reference to create this . Best and simple way to create String is way 1 (String s=“ABC”) using String class