Win a copy of Terraform in Action this week in the Cloud forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Comparing String with StringBuffer

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got caught with this question in a mock exam.
The code is:
------
String str = "Java";
StringBuffer buffer = new StringBuffer (str);
if (str.equals(buffer))
{
System.out.println ("Both are equal");
}
else
{
System.out.println ("Both are not equal");
}
------
The correct answer is "Both are not equal". but what I thaught was, since .equals() method always checks the contents of the objects, will pass the "if" condition.
can any one help me please?
Thnks & regards
 
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 Tausif,
The String class overrides the equals() method of the object class. The java doc for equals() method of String class says

public boolean equals(Object anObject) 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.

Hope this helps you
Reshma
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes it helps to go see the source code of J2SE classes. Below is the code for the equals method of the String class.
As you can see, the method will unconditionally return false if the type of the given argument is not of type String. This makes sense when you come to think about how two objects of different types could ever be equal.
The bottom line is: String and StringBuffer objects are always different as far as equals is concerned.
BUT, the condition would yield true if you would compare str with the content of the StringBuffer and not the StringBuffer itself. To get the content of the StringBuffer just invoke toString() on it.
str.equals(buffer.toString()) would yield true, and thus, satisfy the condition.

[ March 04, 2003: Message edited by: Valentin Crettaz ]
 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin,
I m cool now
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This will produce the sentence: Both are equal
The method equals() is overridden. It will check if the object that buffer.toString() returns is an instanceof String. If this is the case, it will compare the two strings (buffer.toString and str).
My Study Notes
[ March 05, 2003: Message edited by: Karin Paola Illuminate ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i seem to remember that the StringBuffer does not overide the equals method.
so if u swapped it around so that the code read
if buffer.equals(str);
result could be true.
i should check this tho.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper,
Since StringBuffer does not override the equals method, there is no way that the result of
buffer.equals(str);
will ever be true, since a reference to a String object will always be different from a reference to a StringBuffer object.
Remember that the basic implementation of equals compares the references, that is, it performs a == comparison between the two arguments.
 
You showed up just in time for the waffles! And this 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