Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Journal Article - The SCJP Tip Line - Strings, Literally

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The just-released September 2004 edition of The JavaRanch Journal includes an article by Corey McGlone, "The SCJP Tip Line - Strings, Literally".

Please use this thread to comment on and discuss the article.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this mean String literals in the Constant Table/String Literal Pool are never garbage collected? The end of the article seems to imply that. Assuming var is a String and you do..

String foo = "Something" + var

Does this create a new String literal in the literal pool? I thought it did. But if this is the case, and string literals are never garbage collected, I would think you'd see a lot more application servers crash, since over time they could run of memory if this type of declaration was performed. (I know you can remedy the situation above using StringBuffer but I'm curious of the impact the above would have on a server).

Thanks for any info.
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 that are in the pool are Strings that can be determined at compile time. For example: String s = "ja" + "va" will end up in the pool as "java". String a="ja"; String s2=a+"va" will not because the value is evaluated at runtime. ("ja" + "va" uses constant expressions, not variables)

String foo = "Something" + var creates a string "Something" in the pool. If the it were preceded by the statement String var="cool"; there would STILL only be one String put in the pool, because only Strings that can be evaluated at compile time end up there.

It LOOKS like "Somethingcool" could be evaluated at compile time, but it isn't because in expressions involving variables, they are evaluated at runtime, and not compile time.

Here's some stuff (first line is from the referenced article, the rest is from the jls)

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

[ September 18, 2004: Message edited by: Louie van Bommel ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick Reumann:
String foo = "Something" + var


A line of code like that creates just one String literal ("Something"). Because we don't know what String var references until run-time, we can't evaluate this statement at the time the String literal pool is defined (compile-time).

Therefore, no matter how many times you execute the above line of code, you'l never have more than one String literal in the pool. The new Strings are being created on the heap and have no reference to them from the literal pool. The only reference to them is the variable foo. Once these Strings no longer have a reference to them, they are eligible for garbage collection. The literal "Something", however, will never be collected.
 
Techie Pro
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
ur explanation is comprehensive and impressive.
But , i am still confused , especially coz of the article given in the link below :

http://www.javaworld.com/javaworld/javaqa/2003-12/01-qa-1212-intern.html

kindly revert for slaying my confusion.

~Techie Pro~
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Techie Pro", please read our JavaRanch Naming Policy, and change your displayed name to conform with it. We require a displayed name in the format <first name><space><last name>, preferably your real names. Profiles with nonconforming names will be closed after a few posts.
Thanks,
-Barry

(NR)
[ September 16, 2005: Message edited by: Barry Gaunt ]
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello this article is really very good.. thanks for providing such doc for us.
but i am confuse at line where its written like
Just before the main method ends, how many objects are available for garbage collection? 0? 1? 2?
ans is 1...
i want to ask that the object that is now ready for gc is One ?..
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.
According to that obj Two is never eligible for GC.. is it so?

thanks..
Nishita

[ August 31, 2006: Message edited by: Nishita Jain ]
[ August 31, 2006: Message edited by: Nishita Jain ]
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]


I dont think literal pool is same as stack memory. As i have read, the JVM sets a part of heap memory as constant pool.
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so that means in case of Strings there 3 things which play its role.
1. heap
2. stack
3. string literal pool

is it right?

and if yes then stack and string pool will do the same thing
i refered this url
http://www.phptr.com/articles/article.asp?p=31755&seqNum=8&rl=1

Nishita
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this URL,

There is a good explanation of how strings are managed internally.

http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

Regards,
Praveen
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i think i got confused much... now i m very much clear ..
i got that obj named Two is for gc.
and also about stack memory and literal pool..

that link was very helpful..
thank you for great help.

Nishita
 
Bhanurekha Chintagunta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

}
}
 
Bhanurekha Chintagunta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char c = '\u000a'; //linefeed
char c = '\u000d'; //carriage return
char c = '\u0027'; //single quote
char c = '\u005c'; //backslash

are all compile time errors for escape Sequences for a character
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't wake the zombies.
 
Mike Petrov
Greenhorn
Posts: 22
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I've understood the point but one moment in the article still confuses me.
Author says:

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.


Note that the author says "references to the two String literals are still put into the constant table (the String Literal Pool)". Why is it so since the following picture clearly shows that only one is referenced from the Pool, and two is an ordinary-created-object referenced only from Local Variable Table?

BTW, sorry for waking the zombie.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

}
}


hi,
How can we calculate the total numbers of object formed in pool and heap ?
 
Surinder Mehra
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
sorry for waking the zombie, but i am little mystified of explanation of intern() quoted below ,
In the article its mentioned that "Invoking two.intern() will look for a String object referenced from the String Literal Pool that has the same value as the one you invoked the intern method upon. If one is found, a reference to that String is returned and can be assigned to your local variable.".
What if String Literal Pool does not have the same reference/value as the one we invoked on intern ???
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surinder Mehra wrote:What if String Literal Pool does not have the same reference/value as the one we invoked on intern ???

String#intern() javadoc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic