This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "replace" Watch "replace" New topic


Ahmad Mudassir

Joined: Oct 16, 2000
Posts: 10
Hi every one !
What is the output (Assuming written inside main)
String s1 = new String("amit");
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
correct is
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.
jQuery in Action, 2nd edition
subject: replace
Similar Threads
Doubt in Strings
one more on ==
doubt about Strings from Whizlabs
String Creation
about string