11

Java has string pool, due to which objects of string class are immutable.

But my question stands -

What was the need to make String POOL?

Why string class was not kept like other class to hold its own values?

Is internally JVM need some strings or is this a performance benefit. If yes how?

Amol Ghotankar
  • 2,008
  • 5
  • 28
  • 42
  • Think about what will you do if you want to create an immutable class by yourself. – Azodious Jan 09 '12 at 12:31
  • Well my question is - there are so many other immutable classes in java then why java designers decided to make String Pool. Is it a feature to help performance or was it the need of Java Design? – Amol Ghotankar Jan 10 '12 at 05:24
  • Refer following links: https://stackoverflow.com/questions/1881922/questions-about-javas-string-pool https://stackoverflow.com/questions/3653255/some-queries-regarding-java-string-pool https://stackoverflow.com/questions/5457146/regarding-java-string-constant-pool and, you'll get your answer. In one sentence, the answer is `to use JVM memory cleaverly` – Azodious Jan 09 '12 at 12:21

4 Answers4

7

A pool is possible because the strings are immutable. But the immutability of the String hasn't been decided only because of this pool. Immutability has numerous other benefits. BTW, a Double is also immutable, and there is no pool of Doubles.

The need for the String pool is to reduce the memory needed to hold all the String literals (and the interned Strings) a program uses, since these literals have a good chance of being used many times, in many places of the program. Instead of having thousands of copies of the same String literal, you just have thousand references to the same String, which reduces the memory usage.

Note that the String class is not different from other classes: it holds its own char array. It may also share it with other String instances, though, when substring is called.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • **Double is also immutable, and there is no pool of Doubles** Nice pick.. :) Thanks – Aman Gupta Sep 18 '14 at 05:23
  • What do you mean by "String literals a program makes"? Are you saying that JVM internally generates many similar Strings even when the application doesn't work with text and doesn't create many String objects explicitly? – James Sep 24 '14 at 13:44
  • 1
    No, what I'm saying is precisely the inverse thing: if 200 classes use the String literal `""` in their code, only one instance is created, stored in the string pool, and shared by all the classes. – JB Nizet Sep 24 '14 at 17:16
0

What was the need to make String POOL?

When created, a String object is stored in heap, and the String literal, that is sent in the constructor, is stored in SP. Thats why using String objects is not a good practice. Becused it creates two objects.

String str = new String("stackoverflow");

Above str is saved in heap with the reference str, and String literal from the constructor -"stackoverflow" - is stored in String Pool. And that is bad for performance. Two objects are created.

The flow: Creating a String literal -> JVM looks for the value in the String Pool as to find whether same value exists or not (no object to be returned) -> The value is not find -> The String literal is created as a new object (internally with the new keyword) -> But now is not sent to the heap , it is send instead in String Pool.

The difference consist where the object is created using new keyword. If it is created by the programmer, it send the object in the heap, directly, without delay. If it is created internally it is sent to String Poll. This is done by the method intern(). intern() is invoke internally when declaring a String literal. And this method is searching SP for identical value as to return the reference of an existing String object or/and to send the object to the SP.

When creating a String obj with new, intern() is not invoked and the object is stored in heap. But you can call intern() on String obj's: String str = new String().intern(); now the str object will be stored in SP. ex:

String s1 = new String("hello").intern();
String s2 = "hello";
System.out.println(s1 == s2); // true , because now s1 is in SP
Bogdan
  • 623
  • 5
  • 13
0

When we compiler see's that a new String literal has to be created,it first check's the pool for an identical string,if found no new String literal is created,the existing String is referred.

Harinder
  • 11,776
  • 16
  • 70
  • 126
0

the benifit of making string as immutable was for the security feature. Read below

Why String has been made immutable in Java?

Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.

Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files 'password1' and 'password2', respectively. Obviously 'user2' should not have access to 'password1' file.

As we know the filenames in Java are specified by using Strings. Even if you create a 'File' object, you pass the name of the file as a String only and that String is maintained inside the File object as one of its members.

Had String been mutable, 'user1' could have logged into using his credentials and then somehow could have managed to change the name of his password filename (a String object) from 'password1' to 'password2' before JVM actually places the native OS system call to open the file. This would have allowed 'user1' to open user2's password file. Understandably it would have resulted into a big security flaw in Java. I understand there are so many 'could have's here, but you would certainly agree that it would have opened a door to allow developers messing up the security of many resources either intentionally or un-intentionally.

With Strings being immutable, JVM can be sure that the filename instance member of the corresponding File object would keep pointing to same unchanged "filename" String object. The 'filename' instance member being a 'final' in the File class can anyway not be modified to point to any other String object specifying any other file than the intended one (i.e., the one which was used to create the File object).

Punith Raj
  • 2,164
  • 3
  • 27
  • 45
  • This is not really a security reason. The File class would just need to copy the string with the file name into a new String object if strings were mutable in Java. Its only a performance feature that this copying is not needed (and a convenience feature for programmers that they can't forget the needed copying). – Philipp Wendler Jan 10 '12 at 10:07