Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Equal(),==  RSS feed

 
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
 
Ranch Hand
Posts: 262
  • 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.
 
Java Cowboy
Sheriff
Posts: 16084
88
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 ]
 
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!