wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equal/==  help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equal/==  help" Watch "equal/==  help" New topic
Author

equal/== help

Jason sigel
Ranch Hand

Joined: Oct 07, 2003
Posts: 30
Replace return a new string .In the code
class aja{
public static void main(String[] args){
if("String".replace('t','t') =="String")
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
what I know is equals checks the value in object refernce whereas == checks the reference of the object.
class aja{
public static void main(String[] args){
if("String".replace('t','t') .equals("String"))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}

why first code is returning equal.Please correct me.
jaideep
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I suppose the replace method of String class returns an Internalized String, which refer to an existing Similar String if it exists
 
Don't get me started about those stupid light bulbs.
 
subject: equal/== help
 
Similar Threads
Stirngs again... why this happens ?
String literals
String Doubts
Java Strings
String Confusion