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
votes
1 answer

Why is reason that synchronized lock not working on String concatenation

I am using the following code. I am not able to achieve synchronization. As per me String pool concept should have worked here. I want to know the reason behind the problem, not the alternatives to it. import java.util.Date; import…
-1
votes
1 answer

Java variable initializates only once

I have following code: String test = "Test" + System.currentTimeMillis(); When I print out this string several times it shows me exactly the same value. System.out.println(test); Thread.sleep(1000L); System.out.println(test); The result is…
Oleksandr Riznyk
  • 758
  • 1
  • 8
  • 19
-1
votes
1 answer

How Java String pool works? How does Java decide whether to use it or not?

I know that there's a String pool which is supposed to keep some created strings in order to not duplicate them. So, if a user wants to create a string with the same value as another string, it won't be created once again (unless the new String()…
mishkamashka
  • 73
  • 1
  • 6
-1
votes
5 answers

String objects creation

Suppose following is the piece of code, how many string objects are created and where(StringPool or heap memory) Mention the references which are pointing to the appropriate objects. String s1 = "abc"; String s2 = s1; String s3 = new…
Sharath
  • 407
  • 5
  • 16
-1
votes
1 answer

Java Strings Concatenation and Objects

How many objects will be created for the following code? String temp = "a" + "b" + "c" My understanding: a,b and c are string literals and will be stored on the string pool (so no new object created). Then while appending using "+" operator it uses…
learner
  • 1,095
  • 2
  • 18
  • 41
-2
votes
1 answer

String objects and storage

How many objects will be created in the following code and where they will be stored? String s = "abc"; // line 1 String s1 = new String("abc"); // line 2 String str1 = new String("efg"); //line 3
Ankita
  • 27
  • 3
-2
votes
1 answer

Java: How to read a file without bloating the string pool?

Please take a look at the following code snippet: public static void main(String[] args) { BufferedReader reader; try { reader = new BufferedReader(new FileReader( "myfile.txt")); String…
CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
-3
votes
1 answer

Why I am getting different output for string pool while comparing two objects in java?

I am running this program. To check string pool concept. I refer this link and added two more lines with equals method from Object class in java. Why I am getting different output for objRef1==objRef2 vs objRef1.equals(objeRef2). In my opinion they…
krishna
  • 413
  • 2
  • 10
  • 25
-3
votes
3 answers

Comparing two equal Strings after intern() method

Javadoc says that if in the string pool there is an equal String that the intern() method will return the String. public class Demo { public static void main(String[] args) { String str1 = "Apple"; String str2 = new String("Apple"); …
Artmal
  • 348
  • 2
  • 4
  • 16
-3
votes
1 answer

Equality operator bevaiour for dynamically passed string values in java

public class EqualsCheck { /** * @param args */ public static void main(String[] args) { Scanner sc= new Scanner(System.in); Scanner sc1= new Scanner(System.in); String s1 = sc.next(); String s2 =…
ravikant
  • 67
  • 1
  • 1
  • 11
-3
votes
1 answer

Difference among several ways of creating string

Several ways of creating string are shown below. Questions are added following the expressions in the way of comments. String str = "test"; String str1 = new String(str); //Will it invoke the Constructor of String(String)? String str2 = new…
Ryan
  • 213
  • 1
  • 8
-4
votes
1 answer

How a string present in a string pool and a string present in a heap outside stringpool have same hashCode?

Consider the piece of code given below. I was wondering how can a string present in string pool "s1" or "s2" have the same hashCode as a string present in heap as "s3" but outside string pool. class Test{ public static void main(String[] args) { …
-5
votes
1 answer

how to move heap area data to String constant pool?

how to move heap area data to String constant pool ? String s3 = new String("Test"); final String s4 = s3.intern(); System.out.println(s3 == s4);//fasle(i need true) i dont want to create new object so just cut the object…
Shivaraj Mc
  • 25
  • 1
  • 6
1 2 3
8
9