2

As we know that when we create new String object by new keyword like this:

String str = new String("New String Will Have Two Objects");

It will create two objects of, one on java heap memory and other on String pool.

So when we call access "str" which string object is accessed(heap object or string pool object)?

According to my understanding the string pool object is get accessed, if yes then what happens to heap object?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47

3 Answers3

7

If you are creating the String object with new

String str = new String("New String Will Have Two Objects");

In such case, JVM will create a new string object in normal(non pool) heap memory and the literal "New String Will Have Two Objects" will be placed in the string constant pool. The variable str will refer to the object in heap(non pool).

Method ‘intern()’ usage

This is best described by java docs

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.

String str = new String("New String Will Have Two Objects");

str.intern();
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
Nakul91
  • 1,245
  • 13
  • 30
  • Good answer and voted up . However I don't believe `new` makes any difference. – Shahzeb Jul 14 '15 at 05:32
  • 4
    Thanks for vote. :) The little difference between direct String declaration and with keyword new is when you are creating a string with double quotes it will check in pool first if it exists or not. If yes then return the same string. In case of `new` keyword it will always create a new string. – Nakul91 Jul 14 '15 at 05:38
  • @Nakul - In case of use of new keyword would it create new String on pool as well, even if the same is already available? – Gaurav Jeswani Jul 14 '15 at 05:56
  • 2
    It will create an object with the string literal as a parameter to it. It won't create any new String on pool if it is already exists.Rather than it will keep the reference in this case it will make reference count as two, but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one which is already exists – Nakul91 Jul 14 '15 at 06:10
0

new String creates a new string on heap. This string's reference gets assigned to str.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • I have read in java doc that it creates one object on string pool as well, then what happens to that object? – Gaurav Jeswani Jul 14 '15 at 05:24
  • What do you mean, what happens to it? It is there. Every String literal has a corresponding object on the pool, created at the compile time. If you do `String foo = "bar"; String quux = foo;`, there will be only one object, referenced by two variables. If you do `String x1 = new String(foo); String x2 = new String(quux)`, two additional copies will be created on heap, and will get garbage-collected when `x1` and `x2` leave scope. – Amadan Jul 14 '15 at 06:23
0

String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

geddamsatish
  • 174
  • 10