• 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

String/StringBuffer

 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please clear me with the answer?
StringBuffer sb1=new StringBuffer("Hello");
String s1=new String ("Hello");
if(s1.equals(sb1));
System.out.println("Yes");
Whether Yes would be printed or it will give a compiler error as the equals method is used with a String and StrinBuffer object or it will return false?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend ;
this programme will compile because you compare the object. and finally print yes because contain store in the refrence varible are same.
thanks.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it compares the object at all. What it does is to, as your rightly say, compare the *contents* of the object. Since the contents are indeed equal, it outputs "Yes."
Can't see any object comparisions....
David.

*** DOH ALERT ****
Just noticed the ';' after the if statement! Of course it will *always* output "Yes"!!!
Sorry, tried it without the terminator. It doesn't output anything - hence the two string objects are not equal!!!
Sorry for the confusion
[This message has been edited by David Harrigan (edited December 14, 2000).]
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
StringBuffer does not override the "equals" method...... but all these following are true....Can anyone explain..???

Aruna
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String class overrides the equals. The overriden implementation is: (from JDK help...)
"Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object."
Here in this case though both have the same sequence of charaters they are different object types and hence the s1.equals(sb1) fails and prints NO.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the code carefully. There's a semicolon at the end of each if statement. Therefore, all the println statements will execute. That's why you see true printed for everything.
StringBuffer doesn't override equals so it will not exhibit the same behavior as the String class when equals is called.
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should also add that:
s1.equals(sb1) will return false but ...
s1.equals(sb1.toString()) will return true. No surprises.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rishi, This is a trick question! Because there is a ; immediately after the statement if(s1.equals(sb1)), so "Yes" is printed regardless. Thanks to Sam for pointing that out.
Aruna, same applies to your code as well.
However there are some lessons to be learnt here,
1. s1.equals(sb1) in the example will NOT be True because equals() in String returns True only if the argument is another String object and the sequence of characters are same. In this case sb1 is StringBuffer not String. However sb1.toString() returns a String so,
s1.equals(sb1.toString() will be True as both contain "Hello".

2. StringBuffer does NOT override the equals() method, so the method does not work correctly with the stringBuffer objects. Does not matter whether you provide a String or StringBuffer object as an argument.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It helps to have this simple rule:
Comparison of a string to a stringbuffer or vice versa always returns false.

------------------
Regards,
Shree
 
reply
    Bookmark Topic Watch Topic
  • New Topic