Originally posted by Rick Reumann:
... String foo = "Something" + var
Does this create a new String literal in the literal pool? ... since over time they could run of memory if this type of declaration was performed...
Strings created at run-time will always be distinct from those created from String Literals.
Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3).
Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals. Strings computed at run time are newly created and therefore distinct. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents
Originally posted by Rick Reumann:
String foo = "Something" + var
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nishita
Originally posted by Nishita Jain:
[QB] and 1 more thing is String literal pool is same as Stack memory?
because i read somewhere that it also stores the ref to objects.
QB]
Nishita
Nishita
Njoy
Njoy
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
In this case, we actually end up with a slightly different behavior because of the keyword "new." In such a case, references to the two String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table.
Bhanurekha Chintagunta wrote:public class ImmutableStrings
{
public static void main(String[] args)
{
String one = "someString";
String two = new String("someString");
System.out.println("before intern");
System.out.println(one.equals(two));
System.out.println(one == two);
two = two.intern();
System.out.println("after intern");
System.out.println(one.equals(two));
System.out.println(one == two);
}
}
Surinder Mehra wrote:What if String Literal Pool does not have the same reference/value as the one we invoked on intern ???
Joanne
He was expelled for perverse baking experiments. This tiny ad is a model student:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
|