Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

why equals() method not overriden for StringBuffer Class?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class checkBuffer
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
System.out.println("yes");
}
}
}


The equals() method giving false in if statement.
why the equals() method is not working with StringBuffer?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals is working just fine. It's just behaving differently from what you expected.
StringBuffer doesn't override equals so it behaves like equals in Object which means it checks whether the 2 references are the same which in your example they aren't.
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI ,

String a = new String("A");
String b = new String("A");

a==b // it will also return false ...
[ February 07, 2005: Message edited by: rathi ji ]
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body provide me list of some important classes that have overwridden equals() method except String & Wrapper classes ...

Thanks a lot .
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
FYI ,

String a = new String("A");
String b = new String("A");

a.equals(b) // it will also return false ...



Rathi,
With respect to the code above why would a.equals(b) ? return false ... I think it should return true.
I think ( a == b ) // return false
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jay Pawar:


Rathi,
With respect to the code above why would a.equals(b) ? return false ... I think it should return true.
I think ( a == b ) // return false



it should return true, looking at the sourcecode for String.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really soory , Actually I wanted to write == but I don't know what happened ... I really need a break ... sorry guys .
[ February 07, 2005: Message edited by: rathi ji ]
 
Squanch that. And squanch this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic