2

I was asked a question in an interview- How many objects are created on the Heap in the following:

String s1= "A";
String s2= "A";
String s3= new String("A");

I answered 1 - because with the new operator only, a string object is created. When the compiler encounters s1, it will simply create "A" on the string literal pool. And s1 and s2 point to the same literal in the literal pool. But the interviewer confused me by saying that where does this pool exists?

Now, in a certain blog, I read:

"In earlier versions of Java, I think up-to Java 1.6 String literal pool is located in permgen area of heap, but in Java 1.7 updates its moved to main heap area."

So in this way, all the 3 string objects are created on the Heap. Isn't it?

But s1 and s2 point to the same literal in the string literal pool(s1==s2 is true), so a separate object shouldn't be created when s2 is encountered. So in this manner, only 2 objects should be created.

Could someone clarify as such how many String objects are created on the Heap? Am I missing something?

Archit
  • 976
  • 16
  • 28

2 Answers2

1

You are correct. One String object is created by String s3= new String("A"); and put into memory heap. One string literal "A" will be put into String pool.

The allocation will be in the heap but it will still store the String literals separately and object created using new separately.

In earlier version of Java, I think up-to Java 1.6 String pool is located in permgen area of heap, but in Java 1.7 updates its moved to main heap area. Earlier since it was in PermGen space, it was always a risk to create too many String object, because its a very limited space, default size 64 MB and used to store class metadata e.g. .class files. Creating too many String literals can cause java.lang.OutOfMemory: permgen space. Now because String pool is moved to a much larger memory space, it's much more safe.

enter image description here

Source: String-literal and String-object

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
0

The answer is 1. "A" is added to the heap before any of the 3 lines run via the String Pool, which exists in the heap. The first two lines reference those existing values from the string pool. The third line forces the creation of a new object on the heap.

Here's a great write-up: http://www.journaldev.com/797/what-is-java-string-pool

Note: I stand corrected on the comment below. The "A" already exists in the thread pool before line 1 ever runs, so nothing is actually added in line 1. Therefore, the net change to the heap is 1 as you said in the interview since only line 3 actually affects the heap.

Jason W
  • 13,026
  • 3
  • 31
  • 62
  • 1
    Yes - first line of article: "As the name suggests, String Pool is a pool of Strings stored in Java Heap Memory". The change in 1.7 to move to the heap occurred in 2011. – Jason W Sep 25 '15 at 03:41
  • The first line adds nothing to the string pool. The "A" is already there, since the class was loaded. It was originally put into the .class file's string pool by the compiler, and pooled by the classloader. – user207421 Sep 25 '15 at 03:51
  • Thanks @EJP 5. Updated answer to reflect correction. Should have gone to sleep already :) – Jason W Sep 25 '15 at 03:56
  • Got it..! Thanks Jason W and @EJP !! Final count is 1.. Good to know I was correct. :) – Archit Sep 25 '15 at 04:58
  • Hi @JasonW, I have already up voted your answer. – Archit Oct 06 '15 at 19:43