Why String created using new operator creates string literal in string pool
Asked Answered
D

5

8

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".

Dejadeject answered 21/11, 2014 at 6:10 Comment(3)
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.Proclus
"What is the advantage?" No advantage. Only downsides. This is why this use of the String constructor is discouraged.Proclus
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).Proclus
L
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.

Loppy answered 21/11, 2014 at 6:14 Comment(4)
You mean we don't get any specific advantage out of it, so its better not to use new operator to create string.Dejadeject
@RishiKeshPathak - No advantage here..You are creating 2 abcs 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.Loppy
#21622458 But this answer is telling different than yours. And this answer seems more logical to me. @LoppyApprehensible
docs.oracle.com/javase/7/docs/api/java/lang/… -- 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.Apprehensible
P
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.

Petrie answered 21/11, 2014 at 6:12 Comment(0)
C
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?

Clew answered 21/11, 2014 at 6:20 Comment(0)
M
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.

Miocene answered 24/9, 2015 at 14:36 Comment(1)
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.Dejadeject
Z
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

Zebulun answered 24/5, 2022 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.