File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt about Strings  from Whizlabs 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 "doubt about Strings  from Whizlabs" Watch "doubt about Strings  from Whizlabs" New topic
Author

doubt about Strings from Whizlabs

anmita payal
Greenhorn

Joined: Jun 22, 2007
Posts: 23
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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

Joined: Jun 22, 2007
Posts: 23
Thanks marc weber and verma.
praveen oruganti
Ranch Hand

Joined: Feb 05, 2007
Posts: 73
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


Regards,<br />Praveen Oruganti<br />Software Programmer
Joshua Mark
Greenhorn

Joined: Sep 23, 2007
Posts: 15
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?
 
GeeCON Prague 2014
 
subject: doubt about Strings from Whizlabs