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
87
votes
7 answers

How does this Java code snippet work? (String pool and reflection)

Java string pool coupled with reflection can produce some unimaginable result in Java: import java.lang.reflect.Field; class MessingWithString { public static void main (String[] args) { String str = "Mario"; toLuigi(str); …
macrog
  • 2,085
  • 1
  • 22
  • 30
72
votes
7 answers

Questions about Java's String pool

Consider this code: String first = "abc"; String second = new String("abc"); When using the new keyword, Java will create the abc String again right? Will this be stored on the regular heap or the String pool? How many Strings will end in the…
andandandand
  • 21,946
  • 60
  • 170
  • 271
71
votes
1 answer

Why does String creation using `newInstance()` method behave different when using `var` compared to using explicit type `String`?

I am learning about reflection in Java. By accident, I discovered the following, for me unexpected behavior. Both tests as written below succeed. class NewInstanceUsingReflection { @Test void testClassNewInstance() throws…
hanszt
  • 583
  • 1
  • 8
39
votes
7 answers

How many String objects would be created when concatenating multiple Strings?

I was asked in an interview about the number of objects that will be created on the given problem: String str1 = "First"; String str2 = "Second"; String str3 = "Third"; String str4 = str1 + str2 + str3; I answered that there would be 6 objects…
29
votes
1 answer

How to check String Pool Contents?

Is there any way to check, currently which Strings are there in the String pool. Can I programmatically list all Strings exist in pool? or Any IDE already have this kind of plugins ?
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
24
votes
3 answers

Java Strings: private static vs local variable performance

Is there any performance benefit by using a private final static String in java vs using a local string variable that has to get "initialized" every time the method is accessed? I do think that using private static final strings is a good practice…
Francisco Noriega
  • 13,725
  • 11
  • 47
  • 72
23
votes
2 answers

C optimisation of string literals

I've just been inspecting the following in gdb: char *a[] = {"one","two","three","four"}; char *b[] = {"one","two","three","four"}; char *c[] = {"two","three","four","five"}; char *d[] = {"one","three","four","six"}; ...and I get the…
bph
  • 10,728
  • 15
  • 60
  • 135
12
votes
2 answers

String count in the pool with println

I am preparing for the OCA SE 7 exam, and some of these questions are really (!) tricky. In one of the books Im using I found an error I think, so I would like to confirm the following please... public static void main(String... args) { String…
krimat_
  • 142
  • 7
11
votes
7 answers

Why jvm create new string Object each time we create string using new keyword

If jvm creates string pool for memory optimization, then why it creates new Object each time we create string using new keyword even though it exists in string pool?
Harsh Patel
  • 573
  • 6
  • 22
11
votes
1 answer

What happens if String Pool runs out of memory?

What will happen if there are many String literals on String Pool and it runs out of memory. Does it grow its size, if yes how? If not, what will happen if i try to create more String literals?
Milan Pandey
  • 1,010
  • 10
  • 22
9
votes
2 answers

Java String Immutability storage when String object is changed

I understood that if a String is initialized with a literal then it is allotted a space in String Pool and if initialized with the new Keyword it create a String's object. But I am confused with a case which is written below. My question is what if…
Jitesh
  • 1,384
  • 10
  • 20
9
votes
3 answers

Implementing a "string pool" that is guaranteed not to move

I need a "string pool" object into which I can repeatedly insert a "sequence of chars" (I use this phrase to mean "string" without confusing it with std::string or a C string), obtain a pointer to the sequence, and be guaranteed that the pointer…
Chap
  • 3,649
  • 2
  • 46
  • 84
8
votes
2 answers

How Java String pool works when String concatenation?

Beware: I'm not trying to compare if the characters are equals. Because I know how to use the String.equals() method. This question is about String reference I was studying for the OCA exam when I started to learn about the class String and it…
xsami
  • 1,312
  • 16
  • 31
7
votes
5 answers

Why String created using new operator creates string literal in string pool

My Question is what's the use of creating string object in string pool as well as on Heap when we declare String as String a = new String("abc"); What is the advantage ? And why not we create string in heap when we create string as String a = "abc".
RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
6
votes
1 answer

Python Interpreter String Pooling Optimization

After seeing this question and its duplicate a question still remained for me. I get what is and == do and why if I run a = "ab" b = "ab" a == b I get True. The question here would be WHY this happens: a = "ab" b = "ab" a is b # Returns True So I…
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
1
2 3
8 9