3

I'm trying to understand how string pool works.I have gone through many sites and finally i'm more confused now. Let me put down my doubts here. Someone help me in understanding them.

1) Where does the string pool exists? In heap or in method area?

2) String s = "Hello world"; - This would suffice creating a string.Since strings are immutable,once created Strings cant be changed,then what is the need of creating strings using String s = new String(“Hello World”) ?
Though It forces the JVM to create a new String object in heap instead of String pool.But what is the need of forcing the JVM for new String object??

3) When and how strings are getting garbage collected?

Thanks in advance.

Jeevi
  • 2,962
  • 6
  • 39
  • 60

3 Answers3

4

The actual implementation of how strings are managed are not supposed to be consequential in Java, except for the guarantees that:

  1. String x = "hi"; String y = "hi"; // guaranteed that x == y
  2. String x = "hi"; String y = new String("hi"); // no guarantees on == although likely !=
  3. String x = new String("hi"); String y = new String("hi"); // guarantee != but x.equals(y)
  4. String x = "hi"; String y = new String("hi").intern(); // guarantee x == y

Those are the rules for Strings....

In the past (and this will change with Java8), String.intern() and String constants (String x = "hi") were allocated on the PermGen space in the memory model.

As a result, they had different GC mechanisms, and it was possible to run out of memory if you used String.intern() even if you had lots of free heap space (PermGen is/was typically less than 128MB).

Strings allocated with new String(....) are otherwise on the regular heap, and have standard GC mechanisms.

As for why you/we create new instances of Strings using new String ("..."), I can only think of one place where it makes sense to do it that way only, and that is if someone wanted to use the resulting object as a synchronization lock. This can make sense when debugging things..... but not much sense. You have to use 'new' so that you don't inadvertantly use the same String object as some other code for synchronization.

In general Java coding practice, I have typically not seen people using new String ("...."). The reality is that people, for the most part, simply use string concatenation operators, etc. But, that does not mean it is wrong to do new String(...)

When I have looked at the source code for String, I have found that the code relies heavily on String being immutable. So, for example, new String(new String("hi")); only creates one array of char, and it is shared in both String instances. (and the inner instance will be GC'd anyway).

rolfl
  • 17,539
  • 7
  • 42
  • 76
0

1.String pool is NOT is the STACK, and also it is not is the HEAP! It is is the Method Area! String pool belongs to Constant pool. Because we all know, CONSTANT is a value that cannot be changed, the purpose to given a constant pool is to accelerate the speed of program. In your Class file, such as int a = 10, String b = "123450"are being treated as String(You can change them in method). When class is loaded, JVM will put them in an array for maintenance. And put this array in METHOD area named String Pool.

2.When you are creating a String object, you can do: String str1 = new String("abc"), and Stirng str2 = "abc";. They look the same but JVM takes care of them differently. For first one, JVM will create a object of String in HEAP. And return the object to user. But for second one, JVM will check String Pool by using String's equals() method to check if String pool exists the object of String. If yes, return the object of String to user, and it will not re-create a new object of String. If String pool doesn't have the object of String, JVM will create a new object in HEAP and return back to user, at the same time JVM will put it into String pool.

String str1 = new String("abc"); //jvm creates a String object in Heap.

 //jvm can not find "abc" in String pool  
 //Create a String object in heap,and put it in strings pool   
 //Now heap has two string object.
Stirng str2 = "abc";   

 if(str1 == str2){   
         System.out.println("str1 == str2");   
 }else{   
         System.out.println("str1 != str2");   
 }   
  //Print result is  str1 != str2, because they are different objects in heap

  String str3 = "abc";   

 //Now,jvm realizes String Pool already had “abc” object,because “abc”equals “abc”

  if(str2 == str3){   
         System.out.println("str2 == str3");   
  }else{   
         System.out.println("str2 != str3");   
  }   
 //Print result is str2 == str3  



String str1 = new String("abc"); 

str1 = str1.intern();   


Stirng str2 = "abc";   

 if(str1 == str2){   
         System.out.println("str1 == str2");   
 }else{   
         System.out.println("str1 != str2");   
 }  //Print: str1 == str2   
MinGW
  • 87
  • 7
0

String pool is like a bucket which has String constants. For ex:-

Object a = new Object();
Object b = new Object();

It will create two new objects in Heap.

But,

String s1 = "abc";
String s2 = "abc";

It will create only one object in String pool because they are constants.

NirMH
  • 4,769
  • 3
  • 44
  • 69