wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String in VM string-pool 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 "String in VM string-pool" Watch "String in VM string-pool" New topic
Author

String in VM string-pool

See El
Ranch Hand

Joined: Oct 07, 2002
Posts: 44
[From javacoding.net mock exam] :
How many Strings are in the VM string-pool after executing this code fragment :

The answer is 2.
Could anybody explain this ?
I though that in the case above, only 1 string literal is store in the VM string-pool.


See El<br />SCJP2, SCWCD, IBM XML, SCBCD
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well you know that if 2 String references have the same string value at compile time due to the use of a String "literal" if you can call it so, then they both share the same String object, they both point to the same String, this happens in the first 2 lines of code.
In the third line, you instruct the compiler to construct a "new" object of type String with a value - maybe equal to another existing string in the string pool - and assign the reference to a String variable.
String S3 = new String("The same existing String")
result is 2 string objects.
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
At last week's New England Java Users' Group, Joshua Bloch said that there never should have been a String(String s) copy constructor. Since strings are immutable there is no reason ever to make a copy of one.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A good dicussion about Strings:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014328


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String in VM string-pool
 
Similar Threads
need help for a question
String in VM pool?
Help on this Question
string constant pool
Initialization