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 replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "replace" Watch "replace" New topic
Author

replace

Ahmad Mudassir
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Hi every one !
What is the output (Assuming written inside main)
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);
correct is
arit
amit
false
true
but i think it s2 also equal to s3
could any one clear this.
Craig Demyanovich
Ranch Hand

Joined: Sep 25, 2000
Posts: 173
The == operator when used with object references is shallow by default, which means that it returns true only if the references point to the same object. The equals() method, when properly overridden for the type in question, usually works on the "contents" of the object. So, s2.equals(s3) would return true, while (s2 == s3) returns false because s2 and s3 don't refer to the same object.
Read up on constructing String objects with literals versus with the new operator, if you haven't already. There are many nuances with the String class.
Craig
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replace
 
Similar Threads
one more on ==
Doubt in Strings
doubt about Strings from Whizlabs
String Creation
about string