aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String(==) 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(==)" Watch "String(==)" New topic
Author

String(==)

umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
In this code why the output is "not equals"? is it so that String s3 has been created at heep and not in pool, due to which the string r showing references to different object.
class pool
{
public static void main(String[] args)
{
String s1,s2,s3;
s1="Hi";
String s22="hello";
s3 = s1 + s22;
s2 = "Hihello";
if(s2==s3)
System.out.println("equals");
else
System.out.println("not equals");
}
}
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I think that is it. I think even this will have the same results but havent tried it yet:
class pool
{
public static void main(String[] args)
{
String s1,s2,s3;
s1="Hi";
String s22="hello";
s3 = "Hi" + "hello";
s2 = "Hihello";
if(s2==s3)
System.out.println("equals");
else
System.out.println("not equals");
}
}

SCJP
Visit my download page
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
umang,
for s3 = s1 + s22; part append() method of StringBuffer is invoked and the resulting string belongs to heap...that's why
outcome is false.
Randall , during comile time the value of s3 = "Hi" + "hello"; is Known that's why append method is not called and resulting
string belongs to the pool .Anyway I'm not 100% sure and hope that someone will clarify that..
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I compiled and ran the code I posted earlier. It gave an output of "equals".
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ...
Nasir you're right.
In Umang's example the reference is determined at runtime because variables are being used: <code>s1 + s22</code>
In Randall's example, two string literals are used: <code>"hi" + "hello"</code>. These are known at compile time so the resulting string <code>hihello</code> is created. This string already exists in the pool <code>s2 = "hihello"</code>, so the <code>s2</code> reference is returned.
Normally, when strings are constructed using string literals they are stored in the pool. When they are constructed using new or can only be determined at Runtime, the pool is not used.
Hope that helps.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi all!
The == operator is used for reference comparison. As you are creating different objects so reference is different.
Try using the equals() method. This is used for content based comparison.
Regards
Danish Shaukat
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Thanks every 1,
so now if the question is modified with the same code like how many objects are eligible for GC? Then the answer should me just one ie s3, as other objects are created on pool and only this s3 is created on heap isn't it?
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
I think on line s3 = s1 + s22;
two string objects are created...
this line is euqivalent to
s3=new StringBuffer().append(s1).append(s22).toString();
First of all an empty StringBuffer is created (1st object)
then append method is called twice and I think no new object is created here
after appending s1 and s22 to StringBuffer it is converted back to String by calling toString() method and new String object
is returned(2nd String Object)..
so answer to your question should be two string object will be elligible for gc after '}' of main method....
[This message has been edited by Nasir Khan (edited December 17, 2000).]
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Hi Nasir,
I think that only one object is created in the above code and that is after appending s1 and s22 a new String object is returned to s3. I dont think that the empty StringBuffer that u are talking about will b considered.
No doubt in any case the string object will b available for GC only after '}' of the main method. Please correct me if i m wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String(==)