• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

about 'equals'method  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test{
public static void main(String args[])
{
StringBuffer o = new StringBuffer("abcd");
StringBuffer s = new StringBuffer("abcd");
System.out.println(o.equals(s));
}
}
the running result is false,but I thought it should be true, is there any difference between 'equals'method of StringBuffer and that of String?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because there're two objects(two new). try StringBuffer s=o
and you will get true
from api:
public boolean equals(Object obj)Indicates whether some other object is "equal to" this one.
......
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StringBuffer class does not override the equals() method. So it calls the equals() method of the Object class which tests for reference equality. Since the String class and all the primitive wrapper classes override the equals() method, you would expect StringBuffer to also, but not so.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!