Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

String and StringBuffer

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone explain why the both the 'if' conditions fail.
String s = "abcde";
StringBuffer s1 = new StringBuffer("abcde");
if(s.equals(s1))
s1=null;
if(s1.equals(s))
s=null;
I beleive that s and s1 are two distinct objects and to compare their string values, equals() will be the right method. This fails here, howcome ? After looking at the Sun API, StringBuffer inherits the equals method from Object which actually only compares the address (==) and not the string values inside. But again, am I not calling the equals method in my first condition from a string and not a stringbuffer. Where am I making a mistake?
Besides this, correct me if I am mistaken, IO is excluded for the 1.4 exam,right.
Thanks
Sandeep
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A StringBuffer is not a String, nor vice versa. However, if you want to compare the contents of the StringBuffer, with a String you can do that...
 
reply
    Bookmark Topic Watch Topic
  • New Topic