2

I'm curious to know the exact reasons for the behavior of String objects in the following case.

I'm eager to learn about the following snippet and how it works.

Snippet

String s1 = "I belong to String pool";
String s2 = new String("I'm present on Heap space"); 

Question:

  • Where are those string text, that is, character arrays stored in memory? Is the "I belong to String pool" character array present inside String pool and "I'm present on Heap space" character array actually present on the Heap Space?

In short, can anyone please explain the storage locations of String literal reference, String literal text, String object reference s2, String object text etc.

A diagrammatic representation like the following post will be much useful. http://theopentutorials.com/tutorials/Java/strings/string-literal-pool/

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 1
    Any answers to this question will be implementation dependent. Your only guarantee is from [JLS 3.10.5](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5): "a string literal always refers to the same instance of class String [...] strings that are the values of constant expressions (§15.28) - are "interned" [...] using the method String.intern. " – kdgregory Aug 22 '14 at 14:53
  • Please post [new questions](http://stackoverflow.com/revisions/25449739/3) in a new post rather than [morphing](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) the question into something that isn't answered by the existing answers (which *do* answer the current question). –  Aug 22 '14 at 15:49
  • I think [this question](http://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-co) will answer most of your questions. – kajacx Aug 22 '14 at 17:30

2 Answers2

2

In your piece of code, you have 3 Strings:

  • Literal String "I belong to String pool". It will be interned in String pool.
  • Literal String "I'm present on Heap space". It will be interned in String pool.
  • A new String from the result of new String("I'm present on Heap space"). It will create a new String.

    This can be noticed by the implementation of String(String) constructor:

    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }
    

    While the contents (value and hash) of the new string are the same of the original string, it will be a new reference.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    While correct, this doesn't answer the OP's question: "Where are those string text i.e character arrays stored in memory?" – kdgregory Aug 22 '14 at 15:02
  • @LuiggiMendoza I thought local variables are stored on the stack? – flotothemoon Aug 22 '14 at 15:15
  • @1337 yes, but the object reference stored in those variables are in heap – Luiggi Mendoza Aug 22 '14 at 15:15
  • 1
    @LuiggiMendoza Wait, what? So is the reference on the stack or the heap? The actual object probably on heap, reference on the stack, right? – flotothemoon Aug 22 '14 at 15:17
  • @LuiggiMendoza - does that hold true for *all* JVMs? Are you *sure*? – kdgregory Aug 22 '14 at 15:18
  • @1337 references are on the heap, always. This object reference has an *address* to be referenced, and this *address* is stored in the local variable, which remains on the stack. – Luiggi Mendoza Aug 22 '14 at 15:19
  • @LuiggiMendoza I am confused now. You say that the references are always at the heap but the address is stored on the stack. Isn't the address the reference to the object? – flotothemoon Aug 22 '14 at 15:21
  • @kdgregory note that if the interned string is not referenced and used anymore in the application, it can be GCed. – Luiggi Mendoza Aug 22 '14 at 15:22
  • @1337 the object reference is that thing that floats in memory (heap). The local variable just stores a way to access to that part of memory in stack. I guess the wording is what confuses you, because object reference is the term for a new instance of a class. – Luiggi Mendoza Aug 22 '14 at 15:22
  • @LuiggiMendoza So somehwat like a reference to the reference? – flotothemoon Aug 22 '14 at 15:24
  • @LuiggiMendoza +1 Thanks, got it. But I do know what an object reference is, just the stack and heap reference part confused me ;) – flotothemoon Aug 22 '14 at 15:26
2

(You can check this answer, but I will try to make it a bit shorter.)

First thing to realize is that String is an object backed by char[] array, so the String itself can be in different place in memory than the char array containing the actual letters.

From source:

public final class String implements ... {

    /** The value is used for character storage. */
    private final char value[]; // <- String is backed by this array

... 
}

So to your questions:

Where are those string text i.e character arrays stored in memory ?

Both String object and the backing array are stored on the heap, even for compile-time strings. String pool contains only references to the strings.

Does "I belong to String pool" character array is present inside String pool and "I'm present on Heap space" character array actually present on the Heap Space ?

No, everything is allocated on the heap. String pool contains only the references.

In short, can anyone please explain the storage locations of String literal reference, String literal text, String object reference s2, String object text etc.

Let's start with the String objects. There will be 3 string objects, original "I belong to String pool", "I'm present on Heap space" and the newly created "I'm present on Heap space".

However there will be only 2 backing arrays, since the 2nd and 3rd string from previous point will be backed by same char array. (You can look into String(String) constructor).

In the String pool, there will be 2 references: to the original "I belong to String pool" and "I'm present on Heap space"

On your stack, you will have also 2 String references, s1 will be the same as in the string pool (= it will point to the same address), but s2 will be different (because you demanded a new String).

Community
  • 1
  • 1
kajacx
  • 12,361
  • 5
  • 43
  • 70