Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String(==)

 
umang bhartia
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled and ran the code I posted earlier. It gave an output of "equals".
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic