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 Equal(),== 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 "Equal(),==" Watch "Equal(),==" New topic
Author

Equal(),==

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Good afternoon to all,
String,StringBuffer usingwith equal(),==.I couldn't both cocepts.
Any one can you explain crestal clear.
class equal() {
public static void main (String args[])
{
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
//System.out.println(sb1==ss1);
}
}
regards
rex
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Originally posted by rex tony:
Good afternoon to all,
String,StringBuffer usingwith equal(),==.I couldn't both cocepts.
Any one can you explain crestal clear.
class equal() {
public static void main (String args[])
{
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
//System.out.println(sb1==ss1);
}
}
regards
rex



Note that String override the equals method of Object and StringBuffer do not.
For StringBuffer "==" and "equals"1 will return the same result.
And because of that, your result will be: false, false, false.


SCJP 5.0 | SCWCD 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If you use '==' to compare two non-primitive values, you only get true if the values refer to the exact same object - so, for example, you have two variables that refer to the same object:

Most of the time, this is not what you want when you are comparing objects. You usually want to see if two objects represent the same values, even if they are really different objects. You use the equals() method to compare two objects to see if they have the same value.

Note that for the equals() method to work properly, you need to override it, because the equals() method in class Object does the same as '=='.

If you look in the API documentation, you will see that class StringBuffer does not have an overridden equals() method. That means that if you call equals() on a StringBuffer, it will use the implementation in class Object, which just does '=='.

Class String does have an overridden equals() method, but you can only use it to compare the String to another String object. If you pass in any other kind of object (a StringBuffer, for example) it will always return false.

So you cannot compare String and StringBuffer objects to each other directly. If you want to see if the contents of a StringBuffer is equal to the contents of a String, then you have to call toString() on the StringBuffer and compare the two strings:

Your example will print 'false' three times because:

1. sb1 and sb2 refer to different StringBuffer objects, so == returns false
2. StringBuffer has no equals() method, so the equals() method in Object is used, which uses ==, so you get false again
3. The same reason as point 2
[ August 30, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
Thank you Jesper Young...

That made things very clear for me as well!,

Radha Kamesh
[ August 30, 2007: Message edited by: Radhika ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equal(),==
 
Similar Threads
string buffer
String Q
String manipulation
Stringbuffer?
equals with stringbuffer