0

Say I have a function that takes in a string argument like func("abc")

Will "abc" be interned (stored in string pool)? If func("abc") will be called a billion times, will java create a billion copies of "abc" string objects in the pool?

return 0
  • 4,226
  • 6
  • 47
  • 72
  • 3
    It will be stored in the string pool. It will not be created a billion times. That's the point of having a pool. – shmosel Mar 06 '20 at 00:54
  • 1
    No. All identical string literals in your code share a single memory location. You can put `"abc"` in your code a million times, and it will still be all the same string, taking up the same amount of memory as if you had put it there once. – mypetlion Mar 06 '20 at 00:54
  • 1
    It's worth noting that Java has garbage collection, so even if it did create a billion copies of `"abc"` (it doesn't) there would not be a memory leak. – Elliott Frisch Mar 06 '20 at 00:56
  • Memory leaks are very difficult to create in Java. Might be impossible (or at least no commonly known method) now, as I believe that all of the known ways of doing so have been fixed at this point. Further reading [here](https://stackoverflow.com/questions/6470651/how-to-create-a-memory-leak-in-java?rq=1). – mypetlion Mar 06 '20 at 00:58
  • Memory leaks in Java are caused by *holding on* to objects after you really need them. It's the opposite of C and C++, where they are caused by losing pointers to them without having freed or deleted them. – user207421 Mar 06 '20 at 03:14

3 Answers3

2

No. In string pool only one copy will be created and all variable which have same string value, will be pointed to that copy.

You can see below diagram for more clarity:

https://cdn.journaldev.com/wp-content/uploads/2012/11/String-Pool-Java1.png

Ankur Gupta
  • 312
  • 1
  • 3
  • What if the string literal is not assigned to a variable like the function argument in my example? I thought interning happens when it's being assigned to a variable. – return 0 Mar 06 '20 at 01:08
  • @return0 No. Anywhere there is a string literal, interning just happens. – Louis Wasserman Mar 06 '20 at 01:22
  • 1
    Interning happens when a class file is read from memory into the system. It doesn't happen during code execution. – markspace Mar 06 '20 at 01:41
2

Yes, "abc" will be stored in string pool. A new string is not created unless you explicitly create one with new operator.

It is not recommended to force creation of new object. But even when you do, it doesn't lead to memory leak as the new object is bound to certain scope and will be garbage collected when it is no longer referenced

Sudhir
  • 1,339
  • 2
  • 15
  • 36
2

From the Java SE 13 docs, please read the following excerpt:

A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern (§12.5).

String "interning" in Java is storing a String object in a pool so that multiple copies of a string literal in your code refer to the same String object, instead of multiple 'identical' String objects. Perhaps, the following example will clarify it a bit more:

String hello = "Hello"; //A new String object with the value "Hello" is created in the String pool; the variable hello references that object in the pool
System.out.println(hello == "Hello"); //prints true

So in your case, even if you keep calling the method func billion times with the same String literal, only 1 String object with the value 'abc' exists in the pool and all the literals "abc" reference the same object.

for (long i = Integer.MAX_VALUE; i <= Integer.MAX_VALUE; i++)
    func("abc"); //Only 1 String object is created

However, if you explicitly create new String objects from a literal, you might end up having multiple String objects with the same value in the memory until the ones not in context are garbage collected.

for (long i = Integer.MAX_VALUE; i <= Integer.MAX_VALUE; i++)
     func(new String("abc")); //multiple String objects with the same value are created and will continue to exist until garbage collected. 
VHS
  • 9,534
  • 3
  • 19
  • 43
  • 'String "interning" in Java is not merely storing string literals in a string pool': yes it is, and the remainder of this paragraph merely explains what is meant by 'string pool'. The notion of uniqueness is already embedded in 'string pool'. You should remove this paragraph. – user207421 Mar 06 '20 at 03:36
  • @user207421, thanks for the feedback. I edited it with better words. – VHS Mar 06 '20 at 14:46