| 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!
|
 |
 |
|
|
subject: how to know when a String went to the string constant pool
|
|
|