Questions tagged [string-pool]

A string pool allows a runtime to save memory by preserving immutable strings in a pool, so that instances of common strings can be reused throughout the application instead of creating multiple instances of them.

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

Possible duplicate of .

133 questions
5
votes
1 answer

How is string pool measured in terms of buckets in java

While going through this article about String pool and its changes over the years, I came across the following statement: Prior to Java 7u40, the default pool size was 1009 buckets but this value was subject to a few changes in more recent Java…
YetAnotherBot
  • 1,937
  • 2
  • 25
  • 32
5
votes
1 answer

Is String Pool really empty initially as mentioned in the Javadoc of String.intern() method?

Below is the Javadoc comment for String.intern() method: *Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool…
4
votes
4 answers

Why JVM is not "seeing" duplicate String value in String Pool memory?

Possibly this is a duplicate, but couldn't find the explanation I needed. Can someone explain me this. If I'm correct: String s1 = "a"; String s2 = "a"; Both s1 and s2 will point to the same address in String pool and there will be only one object…
Wrapper
  • 794
  • 10
  • 25
4
votes
4 answers

Need to know about String, String Constant pool and String intern method

Say if there are no strings in the String constant pool, and if I say, String s = "Java"; Then how many objects will be created? Now again nothing in the pool, and I say, String s = new String("Java"); Now, how many objects will be created? Now…
RohitT
  • 187
  • 2
  • 11
4
votes
1 answer

String Pool behaves differently when used with concat?

String s1 = "Hello".concat("World"); String s3 = new String("HelloWorld"); //Line-2 String s2 = s1.intern(); System.out.println(s1 == s2); //false System.out.println(s1 == s3); //false System.out.println(s2 == s3); //false If I removed Line-2 and…
4
votes
2 answers

Empty constructor in String class

So I was reading String class when i stumbled on one confusing constructor. The code goes like this public final class String implements java.io.Serializable, Comparable, CharSequence { /** The value is used for character storage.…
Daniel
  • 980
  • 9
  • 20
4
votes
2 answers

About string concatenation behaviours

I understand that, given the immutability of strings, something like String a=""; for(int i=0;i++<9;) a+=i; is highly inefficient, because initially a string is instantiated and put in the string pool, then with a+=i a new string is created (0…
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
4
votes
4 answers

How many Strings are created in memory?

Say I have this String expression String hi = "Tom" + "Brady" + "Goat" I know that the String pool "allows a runtime to save memory by preserving immutable strings in a pool" String Pool How many strings will be created in the string pool? My…
3
votes
1 answer

Java char array to String without creating new objects

When creating a String like this: String s1 = “ABC” the JVM will look in the String pool if "ABC" exists there and create a new object only if "ABC" doesn't exist yet. So the usage of String s1 = new String("ABC") halts this behavior and will…
Sadık
  • 4,249
  • 7
  • 53
  • 89
3
votes
1 answer

Difference between + and += on Java Strings

I would like to understand how memory is allocated when we use + and += on Strings in Java. I know that String literals are stored in the String Constant Pool and in Case #1, both s1 and s2 reference the same memory in String Constant Pool. In Case…
3
votes
0 answers

Password getting stored in java process memory

I was working an issue where password is getting stored in java process memory since the password is stored in a String (hence getting stored in string pool). I made code changes to use char[]. Now the issue is, my application is consuming another…
abyin007
  • 361
  • 2
  • 4
  • 14
3
votes
2 answers

string with no corresponding object in string constant pool while using intern method returns the reference of the same object

Strings with no corresponding objects in string constant pool while using intern method is returning the reference of the same object present in the heap. Isn't supposed to return the reference of an entirely different object which has been newly…
3
votes
1 answer

Java - Unpooled String

Is there a legal way to force JVM not to store particular String instance in a long-lived string pool? Does new String() provide this feature and I can be 100% sure that value created this way will be put into the heap and not into the pool unless…
voismager
  • 433
  • 4
  • 19
3
votes
1 answer

Is this compromise to string immutablity in java?

All of us know that String is immutable in java - content can not be changed once the string is created. String uses character array char[] value to store string content, here is the java code - /** The value is used for character storage. */ …
Harish
  • 169
  • 1
  • 2
  • 7
3
votes
2 answers

Why Wrapper Classes do not have pool Similar to Stringpool?

Integer, Character, Double, etc. -- all these are immutable classes like String. String has Stringpool to save memory but why don't these wrappers have similar pools? I have checked: Integer has a similar pool only up to 127, but not more than that.
Shayan Ghosh
  • 882
  • 6
  • 14
1
2
3
8 9