This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and String Object Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String and String Object Problem" Watch "String and String Object Problem" New topic
Author

String and String Object Problem

Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100


When I run this
OUTPUT:
F
F
F
T
F
F
F
F

Please Explain Why s3 is not == s4 and s3 is not == s5


OCPJP 6-100%; Preparing for GATE11
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

just check how many String objects are created


Thanks & Regards
Sumit Kothalikar
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Gari Jain wrote:

Please Explain Why s3 is not == s4 and s3 is not == s5


Because, s1 and s2 are in the heap, not in the String literal pool, and the object, you've created as s3 is also in the heap. And s4 and s5 are in the String literal pool (One object "abcxyz", two references, s4 and s5).


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rahul S Mishra
Greenhorn

Joined: Sep 06, 2010
Posts: 2
Gari Jain wrote:Please Explain Why s3 is not == s4 and s3 is not == s5

Abimaran Kugathasan wrote:Because, s1 and s2 are in the heap, not in the String literal pool, and the object, you've created as s3 is also in the heap. And s4 and s5 are in the String literal pool (One object "abcxyz", two references, s4 and s5).

Surely all strings are still allocated on the heap? See Javaranch Also as per SCJP guide (Kathy & Bert),
When the compiler encounters a String literal, it checks the pool to sec if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.

"String literal pool" is the key here. s4 & s5 contain reference to same object ("abcxyz" string literal is in the pool). And == looks at the bit pattern (in this case) and bit battern of s4 & s5 is same. s3 is not part of the pool and contains reference to different string object, so bit pattern is different than s4 & s5 and == fails.


Preparing for SCJP 6, target : March 2011.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

When the compiler encounters a String literal, it checks the pool to sec if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.



"String literal pool" is the key here. s4 & s5 contain reference to same object ("abcxyz" string literal is in the pool). And == looks at the bit pattern (in this case) and bit battern of s4 & s5 is same. s3 is not part of the pool and contains reference to different string object, so bit pattern is different than s4 & s5 and == fail


why s3 is not part of the pool ?
once

is executed,i think s3 will refer to a object "abcxyz"
then we



OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

why s3 is not part of the pool ?
once

is executed,i think s3 will refer to a object "abcxyz"


Since, the s1 and s2 are in the heap, the object s3 is also in the heap. Read more about String Literal pool in this link.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i read the link Abimaran refered

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.


the String object
referenced from the String Literal Pool is created when the class is loaded while the other String object is created
at runtime, when the "new String..." line is executed



so s3 would be assigned the value at runtime rather than at compile time
am i right ?

but ,s3 is not created using the new keyword,so it would have reference in string literal pool

as the link says:
the JVM goes through the code for
the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already
referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in
the constant table. Once a reference is made to that String object, any references to that String literal throughout
your program are simply replaced with the reference to the object referenced from the String Literal Pool


so,after line-7 there would be only one entry in the String Literal Pool, which would refer to a
String object that contained the word "abcxyz". Both of the local variables, s5 and s4, would be
assigned a reference to that single String object.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

so s3 would be assigned the value at runtime rather than at compile time
am i right ?

Correct, I think!
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276


would store a newly created string(result of s1 + s2) in the Object heap and return the reference to s3. Though values of s1 & s2 are known at compile time, s3 would be constructed at run-time!
mohitkumar gupta wrote:so s3 would be assigned the value at runtime rather than at compile time
am i right ?

Mohit, you are absolutely correct - this happens at run-time. Other than String literals, every other thing is supposed to be considered as run-time created Strings.
Abimaran Kugathasan wrote:Since, the s1 and s2 are in the heap, the object s3 is also in the heap.

Not exactly. Run time Strings will never refer to the String pool, unless otherwise you use .intern() on them. So, s3 wont be == s4 or s5.


OCPJP 6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Vinoth Kumar Kannan for correcting it!
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
The link helped:

Abimaran Kugathasan wrote:
mohitkumar gupta wrote:

why s3 is not part of the pool ?
once

is executed,i think s3 will refer to a object "abcxyz"


Since, the s1 and s2 are in the heap, the object s3 is also in the heap. Read more about String Literal pool in this link.





ThankYou ALL for replying
 
 
subject: String and String Object Problem
 
Similar Threads
== & equals()
Doubt in Strings
String question
String puzzle
(s1==s5) Vs s1==s5