This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equal(),==

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15208
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper Young...

That made things very clear for me as well!,

Radha Kamesh
[ August 30, 2007: Message edited by: Radhika ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic