0
String s = new String("abc") 

I know that this would create a new String object in Heap. But i am confused about a statement in SCJP book by Kathy Sierra.

It states that the above statement would create an object in heap and at the same if the String "abc" is not present in the String pool , it would also add "abc" to the String pool.

Could anyone please tell me if the object "abc" is also created in the String pool in the above case.

C Snover
  • 17,908
  • 5
  • 29
  • 39
sujith
  • 665
  • 2
  • 9
  • 22
  • 2
    http://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings – Tea Curran Aug 27 '15 at 04:30
  • @Curran : I have gone through the thread mentioned by you before posting the question. However it spoke only about String s = new String() creating an instance on heap even if it exists in String pool . But it did not mention whether String s = new String() would only create a new object or would it add it to the String pool as well. So i thought of posting a new question to clarify my doubt. – sujith Aug 27 '15 at 04:37
  • The book is wrong. The string `"abc"` *is* already in the string pool, by definition. It is placed there by the compiler and classloader. – user207421 Aug 27 '15 at 06:15
  • Well, It is embarrassing but my answer was wrong. sujith please unaccept my answer so that I can delete it. – afzalex Aug 28 '15 at 04:33
  • Strings created using new operator will check first in the pool if found does nothing if not found create in the pool and then create in heap. Here two objects created one in the pool and another in heap. but finally s points to heap object. – Ajay Takur Oct 17 '18 at 14:30

3 Answers3

0

String s = new String("abc")

Yes,above line will create two objects, one in string pool and other in Heap.

So, now

1) If you create a string literal like:-

String s1="abc"; //abc value will be taken from string pool which is previously added 

2) If you create a String object and call intern method, no new object will be created instead it will just refer to "abc" present in string pool.

String s2=new String("abc").intern();

Note: When we are creating new String object based on existing one, it reuses char[] value. Since new String(someStringValue) creates an exact copy of existing string and strings are immutable, there is clearly no reason for the two to exist at the same time.

Even if you have two String objects, they might still point to the same content.

You can refer this:- https://dzone.com/articles/string-memory-internals

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • Is there any specific reason for creating the String in head and again in String pool . By using new String() , if we are explicitly asking to create a new String object , why is it added to String pool along the new String object creation . Isn't it unnecessary usage of additional memory to store 2 copies of it ? – sujith Aug 27 '15 at 04:32
  • It is doing so for optimization and reusing the string values. – Amit Bhati Aug 27 '15 at 04:32
  • Compiler is not recreating the string. It is just keeping a list of variable seperate from the list of values. And these variables will be just like pointers for those values. @AmitBhati – afzalex Aug 27 '15 at 04:35
  • Edited my answer, see in both the scenarios 1 and 2, no new object is getting created instead same value abc which got added in string pool is getting utilized. – Amit Bhati Aug 27 '15 at 04:36
  • @Amit : Does calling intern() method on a string which is initialized using new String() remove the String from Heap and store it only in String pool ? – sujith Aug 27 '15 at 04:39
  • No, it will not remove the object referred by s from the heap, intern method will make the compiler to check in string pool if string value is already present in pool. In case it is present, no new object will be created in heap. – Amit Bhati Aug 27 '15 at 04:42
  • @Amit : So is it like : String s = "abc" and String s = new String("abc").intern() are the same when it comes to the object creation. Both the times it is created only in the string pool . – sujith Aug 27 '15 at 04:44
  • Both are not same, in first case if "abc" is not present then it will only added in string pool whereas in second case if "abc" is not present in pool, two objects will be created one in heap and other one in pool. – Amit Bhati Aug 27 '15 at 04:49
  • Edited my answer, read it things will get cleared to you. – Amit Bhati Aug 27 '15 at 04:53
  • @Amit : one last question . As far as I understood it goes like : String s = "abc" //created in String pool String s2= "abc" //Refers to existing object in String pool String s3= new String("abc") // Now does it refer to the existing instance in String pool without creating a new instance in heap ? String s4= new String("def") // Creates a new String object in head and also in String pool as "def" is not present in the string pool Is the case of String S3 right in my above description ? – sujith Aug 27 '15 at 05:07
  • @Amit : Also if the string already exists in the String pool and then i create a new String(existing String) , and in this case if a new String is not created in the heap , then the reference should be the same . but : String s = "abc"; String s= new String("abc"); s==s1 //false Shouldn't this be the same if both are referring to the same object as the object is already present in the String pool – sujith Aug 27 '15 at 05:15
  • s3 will create a new object in heap. String is a thin wrapper around char[]. – Amit Bhati Aug 27 '15 at 05:31
-1

Yes it does. To optimize memory use, it does so. In case you create another variable with same value "abc", new variable will still point to earlier rather than creating a new one.

Somehow it is trying not to create two equal objects

SacJn
  • 777
  • 1
  • 6
  • 16
-1

Yes, it will be created as you are not having the same string before this line in your code. Now consider this example.

String temp = "abc";            // line1
String s = new String("abc");   // line2

In this case "abc" is not recreated. s will point to "abc" that was created at line 1.

sujith in comment : But in that case i do not see any difference between String temp ="abc" and String temp = new String("abc").

In line 1 a new "abc" will be created in heap. And a new String object will be created and added into the stack. And in line2 a new object of String will be created which will be referring to the "abc" that was created at line1 in heap. To better understand what thing goes to stack and what goes to heap visit here.

Community
  • 1
  • 1
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • In the statement two , aren't we asking to creating a new String object on the heap rather than using the one in the String pool. In the above case , isn't like once the second statement runs , a new String "abc" is created in the heap but it is not added to String pool as the string is already present in the pool. The above statement sounds like a new String() does not create a string in heap if it is already present in String pool. – sujith Aug 27 '15 at 04:43
  • Sujith @afzalex is right by his statement, see how String constructor works:- http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/String.java#String.%3Cinit%3E%28java.lang.String%29 – Amit Bhati Aug 27 '15 at 04:47
  • @Amit : But in that case i do not see any difference between String temp ="abc" and String temp = new String("abc"). String temp = new String("abc") is not creating a new object. It is also referring to the one that is present in String pool . What is the difference between these. – sujith Aug 27 '15 at 04:54
  • No, the second line is creating a new string object by passing the string "abc" from the pool to the constructor of string, creating a new string on heap, puh... understand (-; – Mathias Begert Aug 27 '15 at 05:29
  • 1
    second line is creating new object of string but not creating any `"abc"` again. The new object will also refer to the previous `"abc"` which was created in line1. @sujith – afzalex Aug 27 '15 at 05:31
  • @afzalex : Thank you . This cleared my confusion. – sujith Aug 27 '15 at 05:35
  • 1
    In line 1 `"abc"` is already in the string pool, placed there by the compiler and classloader. All that line 1 does is to initialize the reference `s` to point to that string. In line 2, a *new* `String` object is alliocated. – user207421 Aug 27 '15 at 06:16
  • @EJP If line1 would not be there than `"abc"` would not be created into heap. that's why I said in line1 `"abc"` is added into heap. – afzalex Aug 27 '15 at 06:47
  • 1
    This answer is totally confused and wrong. Line 1 doesn't create any string object at all (because it was already created from the constant pool of the class when the class was loaded), so certainly not on the heap. And objects are *never* created on the stack, the stack only contains references to objects. – Erwin Bolwidt Aug 28 '15 at 02:03