13
String h = "hi";

here we are referencing string h to string literal hi. JVM has a string literal pool to store string literals so we can reuse the strings as they re immutable...

When we say reusable, what is the exact meaning of this? are we talking about the address ? that it is picked from same address evey time ?

nr5
  • 4,228
  • 8
  • 42
  • 82

4 Answers4

13

Yes, to make things simpler you can think of it as picking from same address, but to be more precise variables are holding same reference which is number/objectID which JVM use while mapping to proper memory address of object (object can be moved in memory but will still have same reference).

You can test it with code like this:

String w1 = "word";
String w2 = "word";
String b = new String("word"); // explicitly created String (by `new` operator) 
                               // won't be placed in string pool automatically
System.out.println(w1 == w2);  // true  -> variables hold same reference
System.out.println(w1 == b);   // false -> variable hold different references,
                               // so they represent different objects
b = b.intern(); // checks if pool contains this string, if not puts this string in pool, 
                // then returns reference of string from pool and stores it in `b` variable
System.out.println(w1 == b);   // true  -> now b holds same reference as w1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • b = b.intern(); // put string into pool of strings, return it from pool, then store it in b refrence.... put it in pool ok rest not understood :( – nr5 May 25 '12 at 19:10
  • I need to store somewhare value from `b.intern()` because String is immutable (can't be changed) so I do `b=b.intern()` thats why i wrote "store it in b refrence" – Pshemo May 25 '12 at 19:22
  • I was just reading some stuff.. when u say string cant be changed once it is created, whats does that mean ? [sorry 4 amateur questions] – nr5 May 25 '12 at 19:30
  • 2
    No problem [I am amateur 2]. Immutable = there are no methods to change value of object. In String case you can see methods like substring and it could seem that it does something on your String, but in reality it creates new one, then returns it as result. Original String from which you invoked substring method (or any other method) is untouched. – Pshemo May 25 '12 at 19:39
9

In the case of

String h = "hi";
String i = "hi";
String j = new String("hi");

Depending on the version of the JDK the compiler may do what is called interning and create a single instance of the byte data that represents the "hi" and reuse it between the to variable references. In the most recent specifications all String literals are interned into the String pool in the Permanent Generation.

Using the new keyword as in the last statement will create a new instance of the exact same bytes as a separate object.

String objects created at runtime are not in the String pool unless .intern() is called on them. This is usually not needed and can cause problems, rarely has any significant benefits.

h == i; // true
h == j; // false
j.intern();
h == j; // true
  • String h = "hi"; and String i = new String("hi"); .... something different happens in this case ? – nr5 May 25 '12 at 18:59
  • so that the content "hi" of h and i is stored at one address and content "hi" of object j is stored at another address am i correct ? – nr5 May 25 '12 at 19:39
5

What is meant is that if 20 objects use the same literal String:

private String h = "hi";

all these objects will in fact reference the same String instance in memory. And it doesn't matter, because it's impossible to change the content of the String, since it's immutable. The same instance can thus be shared without problems between objects.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
4

There's a String literal pool inside the JVM with all the Strings created during the lifetime of the program. By reusable, it is meant that, when you want to use another String with exactly the same characters and encoding, a new String object is not created. Instead, the reference will point to the already existing String in the string pool. This is called interning.

This can be done because Strings are immutable in Java.

The reason for doing something like this is to avoid the cost of creating a new String object.

Look here for more details about what the String literal pool is and how it works: http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

Also, look at this question: Shouldn't I do `String s = new String("a new string");` in Java, even with automatic string interning?

Community
  • 1
  • 1
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69