GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Journal Article - The SCJP Tip Line - Strings, Literally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Journal Article - The SCJP Tip Line - Strings, Literally" Watch "Journal Article - The SCJP Tip Line - Strings, Literally" New topic
Author

Journal Article - The SCJP Tip Line - Strings, Literally

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Aug 17, 2004
Posts: 76
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

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Techie Pro
Greenhorn

Joined: Sep 15, 2005
Posts: 1
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

Joined: Aug 03, 2002
Posts: 7729
"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 ]

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 Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
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 ]

Nishita
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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

Joined: Mar 30, 2006
Posts: 97
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

Joined: Jul 30, 2006
Posts: 138
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

Joined: Mar 30, 2006
Posts: 97
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

Joined: Jul 28, 2008
Posts: 4
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);

}
}


Njoy
Bhanurekha Chintagunta
Greenhorn

Joined: Jul 28, 2008
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343



Don't wake the zombies.


"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
Mike Petrov
Greenhorn

Joined: Oct 10, 2011
Posts: 22

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

Joined: Feb 20, 2013
Posts: 176
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

Joined: Aug 06, 2013
Posts: 32
    
    1
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

Joined: Aug 05, 2005
Posts: 3602
    
  15
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


Joanne
 
GeeCON Prague 2014
 
subject: Journal Article - The SCJP Tip Line - Strings, Literally