• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

String and String buffer

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha anna..kindly give me answers to 2 questions:
1.How are string Buffer and String object different ? see the following code :
StringBuffer s1= new StringBuffer("JAVA");
StringBuffer s2= new StringBuffer("JAVA");
if s1.(equals(s2) )
System.out.println( s1 = s2)
else
System.out.println(s1 # s2);
The output is s1#s2.
However if I use String instead of String buffer the answer is
s1 = s2.
2.Also why does comparison with "==" yield different result from .equals() method.
Please explain Maha Anna !! Thanks in advance

 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I request all others also to participate. After all your share, if anything left for Maha , I will finish up. Generally I welcome others to participate first. If there are no reply at all , then I try to answer.
regds
maha anna
[This message has been edited by maha anna (edited April 19, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

StringBuffer inherits the equals() method from the
Object class and so the method does not perform
a char by char comparision.
Re your next question, it has been beaten to death
on the ranch, please do a search on "string" as your
subject and it is all there...hey no pains no gains
Regds,
- satya
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himabindu Devulapalli,
Did u find out? Please reply. If not I can explain.
regds
maha anna
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himbandhu,
The StringBuffer class (String aswell)inherit equals() method
from Object class.
The important difference is String class overrides this method
to do character by character comparision.
Whereas the StringBuffer class doesnot override this method.
So your ans gives they are not equal.
rgds
Girish
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since lost of people have answered your first
question( why .equals() returns false ) I am
going to skip that one.

Next question - However if I use String instead
of String buffer the answer is s1 = s2.
It is because of String pooling
Java does. Remember Strings are reused by
Java if you created them using = operator
instead of new().
Next Question
2.Also why does comparison with "==" yield
different result from .equals() method.
Generally == compares references, not the
contents. So for reused strings, because the
strings are pointing to the same object(address)
== is returning true.
Does this answer your questions?
Good luck.
 
Himabindu Devulapalli
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satya, Girish and Maha Anna. I havd also looked up this mornign the Api for String and string buffer class. Thanks again one and all for your time and patience.
:-) Himabindu D
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic