Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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

doubt about Strings from Whizlabs

 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1=new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3 = "arit";
String s4 = "arit";
String s2 = s1.replace('m','r');
System.out.println(s2 == s3);
System.out.println(s3 == s4);

My answer is arit amit true true , but the answer is arit amit false true.

What I understood is s2,s3 and s4 points to the same memory area. Anybody can explain me these memory area in detail ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s3 and s4 reference the same String object, because it is a String literal "arit" in the String pool. Therefore, s3 == s4 will return true.

s2 also references a String with a value "arit", but this is a different object, because the replace method creates a new instance at runtime. Therefore, s2 == s3 will return false.

See Strings, Literally.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Objects,Strings == compares references.Not value
When we do ,String s2=s1.replace('m','r'); it returns a new String string..given to s2.
String s3="arit";
one more string given to s3.
both strings have same value ,but == sees references.which are different.
 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks marc weber and verma.
 
praveen oruganti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class praveen
{

public static void main(String args[])
{
String s1=new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3 = "arit";
String s4 = "arit";
String s2 = s1.replace('m','r');
System.out.println(s2.equals(s3));
System.out.println(s3 == s4);

}
}

output:
arit
amit
true
true
 
Joshua Mark
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I am wrong here, but doesn't this basically deal with where the object is located in memory?


From the book.....when you call String s = new String("amit"); A new string object is placed in heap memory. But when you call String t = "amit", there is no object contained on the heap, instead, you are creating a String literal contained in the String Pool Memory. So as someone previously said, when you use == you are comparing the references, since the references are each pointing to two different sets of memory which don't know about each other the answer is false.

Isn't this this real reason it returns false or am I off?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic