• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== & equals( ) in StringBuffer

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
if(sb1.equals(sb2))
I think it returns false as StringBuffer doesn't override equals() method.If I am wrong please correct me.
What is the result if it is like this.
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=sb1;
what does the following comparisons return?
1)if(sb1==sb2)
2)if(sb1.equals(sb2))
please help me.Thanks in advance.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the first Question is correct !!
The answer to 2a) is it returns true as there is only one
reference at sb1 !!
The answer to 2b) is also true as it is the same stringbuffer
object
Praveen
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Q.StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
What does this return?
if(sb1==sb2)
Q.String s1=new String("abc");
String s2=s1;
what does the following comparisons return?
A.if(s1==s2)
B.if(s1.equals(s2))
please give me the answer.Thanks in advance.
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the first one is false because sb1 and sb2 are two different references.
s1 == s2 is true since both refer to the same reference.
s1.equals(s2) is true because if the two string references are same then this has to be true.
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot carl.
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic