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
3
votes
2 answers

Can a String be added to string pool during run-time?

If I get a string object from a method call or from StringBuilder.toString(), does this string gets added to the String pool? Does String get added to the pool only during class load?
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
3
votes
3 answers

Does StringBuilder.append() method fill up the string pool?

I know that using most of String's methods will return a new string object which is added to the string pool and that the string literals passed as arguments to these methods also are added to the string pool. For example the code below will create…
user9349193413
  • 1,203
  • 2
  • 14
  • 26
3
votes
2 answers

Will the String passed from outside the java application be saved in String pool?

I read many answers but none of them really answers my question exactly. If I've a java service running on some port and a client connects to it and calls a method like: String data = getServiceData("clientKey"); Now my question is, will this…
Heisenberg
  • 5,514
  • 2
  • 32
  • 43
2
votes
0 answers

Python multiple strings point to same heap memory object if value have less than 4 DIFFERENT characters

in addition to the answers provided here For example : q = "asdasdasdsadsadsadsadsadsadsadsadsad" a = "asdasdasdsadsadsadsadsadsadsadsadsad" >>> a is q True even though the strings are long(way longer than 4 characters), python still points to the…
2
votes
2 answers

Are string objects and their literals garbage collected?

I have some questions revolving around the garbage collection of string objects and literals and the string pool. Setup Looking at a code snippet, such as: // (I am using this constructor on purpose) String text = new String("hello"); we create two…
2
votes
1 answer

How does String concatenation work in terms of the String Pool?

13: String a = ""; 14: a += 2; 15: a += 'c'; 16: a += false; 17: if ( a == "2cfalse") System.out.println("=="); 18: if ( a.equals("2cfalse")) System.out.println("equals"); Output: equals Please correct me if I am wrong... At line 13 a new String…
S. Tiss
  • 139
  • 1
  • 9
2
votes
1 answer

What's the point of creating and having an opportunity to create a String object out of string pool

I know that String objects are immutable, and I can create String object in heap (String s1 = new String("text1") and in string pool (String s2 = "text2"). So what's the point of having an opportunity to create a String out of string pool? Why Java…
Denys_newbie
  • 1,140
  • 5
  • 15
2
votes
3 answers

What memory (or performance) issues does the two approaches have?

Being a novice Java developer and having read about the String class and still somewhat confused about it's functionality. So, I decided to write a method to capitalize a user's name in the form, john changes to John using the code below: 1st…
Marome
  • 47
  • 1
  • 11
2
votes
0 answers

What does String intern() method do?

I read quite some answers about the String.intern() method but didn't quite get my answer. The intern method is a native method with the docs saying : Returns a canonical representation for the string object. Now String is a wrapper around the…
Priyak Dey
  • 1,227
  • 8
  • 20
2
votes
1 answer

Where will be the newly created String? Heap memory or String constant pool?

As per Java, There are two places in which strings are stored. String literal pool and heap memory according to its creation. I need to know, when assignment of string is done to another string, where does the newly created string will be…
2
votes
0 answers

Eclipse String cache is wrong, == does not work as expected

I have a simple application with a main method that compares two literal strings: public class App { public static void main (String[] args) { String first = "a"; String second = "a"; System.out.println(first == second); …
Lucas Yoshioka
  • 87
  • 1
  • 1
  • 4
2
votes
2 answers

How many object will be created in the String Constant Pool and heap

How many object will be created in the String Constant Pool and heap for the following code : String s1 = "Stack"; String s2 = s1 + " Overflow"; As per my knowledge all literals are created in the String Constant pool, But the string concat…
aditi jain
  • 21
  • 1
2
votes
0 answers

How Java JDK create new Strings in String Pool

I have learned that when you have assigned the value with strings, it use a string pools. So that, they are using same address String S1 = “Hello World”; String S2 = “Hello World”; String S3 = “Hello”; String S4 = “ World”; System.out.println(S1…
Kunanon S.
  • 31
  • 1
  • 6
2
votes
4 answers

== with Enum name() and toString()

Can anyone explain why toString() and name() are referencing the same String? when I used == to compare them with a String literal they all pass! How does enum name work with String pool from JVM? static enum User { BASIC,…
peter
  • 8,333
  • 17
  • 71
  • 94
2
votes
1 answer

String pool object creation confusion

I tried some thing like below : String s1="test5"; String s2="test"+5; String s3="test"+s1.length(); System.out.println("s1==s2 "+(s1==s2)+" s2==s3 "+(s2==s3)+" s1==s3 "+ (s1==s3)); The output is: s1==s2 true s2==s3 false s1==s3 false Why s2 and…
Sangam C.
  • 41
  • 4
1 2
3
8 9