File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to know when a String went to the string constant pool Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "how to know when a String went to the string constant pool" Watch "how to know when a String went to the string constant pool" New topic
Author

how to know when a String went to the string constant pool

Jeane Lindford
Greenhorn

Joined: Jun 20, 2011
Posts: 16

hi all
how to know when a String went to the string constant pool
for example



if I pass "new BoomBox2().go(new String[]{"x"});" both SOP are executed
but if I execute this code via commandline like

java BoomBox2 x

only the first SOP are executed
why in this second invocation the x passed via command line arg does not went to the string constant pool?
how do I know that a String comparison via == will return true or false?
thanks
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Jeane, to the best of my knowledge the second invocation
java BoomBox2 x
returns an array containing a new String object;when you use the new keyword to create a string, the object resides in the heap, which off course has a different "address" from the string-constant-pool, hence the strings reference variable refers to that object in the heap, and NOT the string-constant-pool.
The == returns true if both references refers to the SAME object.
The equals() method returns true if and ONLY if the reference variable type and object types are equal. java implemented the equals() method in Strings and Wrapper classes in such a way that it returns true if the reference AND object types are MEANINGFULLY equal eg;


Output


HTH

Regards

Ikpefua


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Hi Jeanne,

I don't know how you are meant to know either, I was asking the same question as you. When you pass a String via the command line, then a new String is created (or it looks like it). Is this ALWAYS? It is because it is?


OCA, OCP Java 6
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Guys, I found a link that explains in full details EVERYTHING you need to know about Strings, String-Constant-Pool etc... Take your time and read carefully: STRINGS, LITERALLY
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
VoilĂ :
Strings created at run-time will always be distinct from those created from String Literals.


Cheers once again, Ikpefua .

Ruobo Wang
Greenhorn

Joined: Jul 27, 2011
Posts: 11
this thread's info helped a lot , thank you!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to know when a String went to the string constant pool
 
Similar Threads
How many Strings in pool?
Is Constructor is invoked during object creation
Error when using new ForkJoin feature
interface
Object initialization sequence