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
1
vote
2 answers

How to add a string to the string pool

When a class is loaded by the bootstrap class loader from a class file, it adds to the string pool the strings contained in its constant pool. However, what if I build my own class loader? How to ask to add a string literal into the string pool? I…
Codoscope
  • 892
  • 1
  • 10
  • 18
1
vote
0 answers

How does Java check if string exists in the String pool?

I referred to a few posts on Stackoverflow and other sites about Java string pool. So, when we do: String s1 = "Emilia"; It creates a string object in the string pool. [Assumming we started from an empty string pool] Now when we do, String s2 =…
Bhargav Jhaveri
  • 2,123
  • 1
  • 18
  • 23
1
vote
3 answers

How is memory alocated with Strings in Java?

Having the following code: String s="JAVA"; for(i=0; i<=100; i++) s=s+"JVM"; How many Strings are created? My guess is that 103 Strings are created: 1: the String "JAVA" in the String pool 1: the String "JVM" also in the String pool 101: the…
Adamos2468
  • 151
  • 1
  • 9
1
vote
3 answers

String pool and String[]

public static void main(String[] args) { String[] arr = new String[5]; for (int i = 0; i < arr.length; i++) { arr[i] = "aaa" + i; } System.out.println(arr[0] == "aaa0"); // false String s = "aaa0"; …
Tim Florian
  • 408
  • 1
  • 3
  • 13
1
vote
3 answers

Confuse about String reference comparison == with intern

I read this when should we use intern method of string on string constants but still not very clear with String == compare also with intern(). I have a couple examples. Can someone help me understand this better. String s1 = "abc"; String s2 =…
peter
  • 8,333
  • 17
  • 71
  • 94
1
vote
4 answers

JAVA : How many Objects will be created ? why?

I was going through SCJP examination and found one line in one book. String s = new String("abc"); And it was written that, two objects will be created on above line. one on HEAP and one on STRING POOL. I am not satisfied by the declaration given…
Tilakraj Jayswal
  • 471
  • 4
  • 10
1
vote
1 answer

Run-time constant pool GC collection

Does GC runs on run-time constant pool? If yes, when? If no, what happen if the string pool is full with string literals? I did google but dint find any relevant thing on this problem also tried with program on my machine.
Amit
  • 497
  • 3
  • 8
  • 24
1
vote
1 answer

Strings and Permgen memory

I have a map of format Map stored in a file. This file has over 100,000 records. The value of each entry is nearly 10k. I load 1000 records into a map in memory , process them ,then clear the map and load the next 1000 records. My question is :…
sujith
  • 665
  • 2
  • 9
  • 22
1
vote
2 answers

Where is the String literal which was passed in the constructor, stored when we create a new String object

In Java when we write String S1 = "TestString"; String S2 = "TestString"; and then compare with if(S1==S2), we get true as the boolean result. The explanation for the same being that the String constants are created in the String Pool and hence it…
Navneet
  • 81
  • 1
  • 8
1
vote
2 answers

String pool memory alloaction

String s = "abc"; String s4 = s + ""; System.out.println(s4 == s); System.out.println(s4.equals(s)); This prints: false true Can anybody please explain why is it so?
user153
  • 148
  • 1
  • 2
  • 10
1
vote
2 answers

Create New Strings in normal memory and in string pool

If for example: String str1 = “abc”; String str2 = new String(“def”); Then, Case 1: String str3 = str1.concat(str2) will go in the heap or pool? Case 2: String str4 = str2.concat(“HI”) will go in the heap or pool?
1
vote
5 answers

How may String Objects are created in java (By the following code)

How many String objects are created in java by the following code: if there is no String object in the String pool containing the same value . (I read somewhere that Since we are passing arguments as "Hello", which is a String literal, it will…
0
votes
0 answers

Comparing String literal to String object's literal in Java

When we create a String using = we create (if it does not exist) a new String literal in the String pool. I know the String pool contains only unique values. Step 1: String var1 = "Java"; OK. We now have 1 literal in the String pool and 1 object…
Sv_BS
  • 1
  • 1
0
votes
2 answers

what will happen if our String Memory is full in Java 8 in a real life application

I am currently working on java 8 Project from last 4 years. In an interview, I was being asked what will happen if your String pool is full. Never encountered it. Already searched a lot didnt find any satisfactory answer for real life app.
NEHA
  • 9
  • 1
0
votes
2 answers

Java Strings - What is the difference between "Java" and new String("Java")?

Given this example code: class basic { public static void main(String[] args) { String s1 = "Java"; String s2 = new String("Java"); } } Are s1 and s2 both reference variables of an object? Do those two lines of code do the…
Andrew
  • 11
  • 3
1 2 3
8 9