7

My Question is what's the use of creating string object in string pool as well as on Heap when we declare String as String a = new String("abc"); What is the advantage ?

And why not we create string in heap when we create string as String a = "abc".

OO7
  • 2,785
  • 1
  • 21
  • 33
RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
  • That `new` does *not* create a string pool entry. The literal does that before you even call `new`. If you do `new String(a)`, nothing is created in the string pool. – Thilo Nov 21 '14 at 06:33
  • 2
    "What is the advantage?" No advantage. Only downsides. This is why this use of the String constructor is discouraged. – Thilo Nov 21 '14 at 06:35
  • Also note that the compiler does a similar thing for primitive wrappers. `Integer x = 123;` also does not create an object in the heap (it takes it from a pool, same as with Strings). – Thilo Nov 21 '14 at 06:36

5 Answers5

6

The java language was designed like that. Anything you use between double quotes is a compile time constant and goes into the String pool. So, in your case :

String a = new String("abc");

"abc" will be resolved as a compile time constant and thus will be added to the String constants pool for the current JVM.

Next, the value of a will be resolved at run-time and will be added to the heap during run-time.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • You mean we don't get any specific advantage out of it, so its better not to use new operator to create string. – RishiKesh Pathak Nov 21 '14 at 06:39
  • 3
    @RishiKeshPathak - No advantage here..You are creating 2 `abc`s one in heap and another on String constants pool.. in this case, there is no advantage.. actually you are creating additional objects for the GC to work on.. If you had done `char[] c = {'a','b','c'}; String s = new String(c);` then `abc` will not be in the String constants pool. SO, you will be good to go. – TheLostMind Nov 21 '14 at 06:42
  • https://stackoverflow.com/questions/21621958/what-happens-with-new-string-in-string-constant-pool But this answer is telling different than yours. And this answer seems more logical to me. @TheLostMind – Mukit09 Jul 30 '19 at 10:24
  • https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern() -- this site is also saying, "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." So **new String("abc")** don't put "abc" in the pool unless intern() is called. – Mukit09 Jul 30 '19 at 10:27
3

First, I recommend that you not use new String("abc") because it behaves as you described. Second, when you use new you should expect a new Object instance will be created and it is.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

First of all let me clear you when you write

    String str=new String("abc"); 

new object is created irrespective of content in the variable. Secondly when you create String using

    String str="abc"; 

at this time this content will be searched in the pool. If any string matching same content as that of new one then only reference will be created on stack but it will point to older one heap location. Got it?

Aditya Ekbote
  • 1,493
  • 3
  • 15
  • 29
0

I believe string object creation by using new operator, don't create object in string constant pool due to the below 2 reason.

  1. Intern() method is used to add the string object to string constant pool. If string object is present in string constant pool, then there is no use of intern() method.

  2. String literal = "abc"; String object = new String("abc"); System.out.println("result = " + literal == object); // false

If string object is present in string constant pool at compile time, then the result should be true.

Please correct me if I am wrong.

  • your both point is result of Creating String with new operator. I suppose actually we should never use string in real world, it has no advantage only downsides. But it exits only because String is a Object and can be initialized using constructor. read top answers for details. – RishiKesh Pathak Sep 25 '15 at 14:32
0

When you use a new keyword the string doesnot go to string pool. For example when you do String c = new String("David"); String d = new String("David"); Here you will have two reference variables c and d both pointing to two different objects having same value "David". But when you do String a ="Rahul"; here "Rahul" lives in the string pool. Now again if you do String b= "Rahul"; first Java will search if there is "Rahul" living in the string pool, and there is. So now reference variable b will point to the same "Rahul" living in the string pool. So here you have reference variable "a" and "b" both pointing to the same "Rahul" in the string pool. But since strings are immutable changing "a" won't have any impact on "b" or vice versa. So you can use any one you like based on your need and requirement