dcsimg
Why the code doesn't work for StringBuffer ?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, July 14, 2003 03:38 AM

Following is the code whcih checks for the equality of 2 StringBuffer objects .But ,it doesn't give the output as "Same buffers" .Why so? But ,this code works fine with String objects ...that is also I am not getting ? Please ,give me the reason for it. public class CompareStrings { public static void main(String args[]) { StringBuffer str1 = new StringBuffer("Hi"); StringBuffer str2 = new StringBuffer("Hi"); // compare the StringBuffer objects if(str1.equals(str2)) { System.out.println("Same Buffers"); } // if end } // main end } // class end    More>>
			
Following is the code whcih checks for the equality of 2 StringBuffer objects .But ,it doesn't give the output as "Same buffers" .Why so? But ,this code works fine with String objects ...that is also I am not getting ? Please ,give me the reason for it.


public class CompareStrings
{
public static void main(String args[])
{
StringBuffer str1 = new StringBuffer("Hi");
StringBuffer str2 = new StringBuffer("Hi");

// compare the StringBuffer objects
if(str1.equals(str2))
{
System.out.println("Same Buffers");
} // if end

} // main end

} // class end
   <<Less

Re: Why the code doesn't work for StringBuffer ?

Posted By:   Anonymous  
Posted On:   Saturday, September 27, 2003 07:56 AM

Hi Deepali P.




please note that the StringBuffer class doesn`t

override the equals() method for these reason two objects

of StringBuffer compared with equals() will result false
even if the values are the same so the boolean expression inside the if() statement will result false. in
oposite the String class override these method (equals()) so if compare
two String objects with equals if they have the same value

it will return true.




hope i could help.

Re: Why the code doesn't work for StringBuffer ?

Posted By:   shivnarayan_varma  
Posted On:   Saturday, August 2, 2003 06:54 AM

Hi friends,


As u are all aware of that String are immutable objects. Hence they never changes once created in memory.
Hence If I declare String object like this:


String str1 = "ABC";
String str2 = "ABC";




It will create only one instance in the memory.

Both the reference str1, str2 will point to "ABC" only.





Hence str1.equals(str2) results true, becose Both
references str1 & str2 are pointing same instance.




str1---->ABC<-------str2
Same pointer


But in case of StringBuffer they are mutable.


StringBuffer str1 = new StringBuffer("ABC");

StringBuffer str2 = new StringBuffer("ABC");



str1----->ABC

str2----->ABC



Different pointer


will create different instances in the memory.


Hence str1.equals(str2)--(swallow comparions) will return false. We can ovoid this situation by overiding equals method.


Hope u would understand it.




Regards

Shiv

Re: Why the code doesn't work for StringBuffer ?

Posted By:   Anonymous  
Posted On:   Monday, July 14, 2003 05:07 AM

Hi Deepali,

Both String and StringBuffer inherit equals() method from Object class.

equals() method of Object checks if the references we are comparing are pointing to the same object. So this is same as using '=='. No deep comparision.

The diff. between String and StringBuffer equals() method is that, String class overrides the Object's equals() method whereas StringBuffer does not do this.

So in String class, equals() method does deep comaparision, i.e., it checks for the value in the String object. And StringBuffer's equals() method does shallow comparision- checks if both the objects are pointing to the same object..



StringBuffer:(Object's equals() method)

StringBuffer str1 = new StringBuffer("Hi");

StringBuffer str2 = new StringBuffer("Hi");

str1.equals(str2) - shallow comparision - returns false

str1 == str2 - shallow comparision - returns false



String:(overriden equals() method)

String str1 = new String("Hi");

String str2 = new String("Hi");

str1.equals(str2) - deep comparision - returns true

str1 == str2 - shallow comparision - returns false


hope u got it.....

Re: Why the code doesn't work for StringBuffer ?

Posted By:   Bahman_Barzideh  
Posted On:   Monday, July 14, 2003 04:34 AM

StringBuffer does not define its own equals ()
method. It inherits equals () from its parent, Object. Object.equals () returns true
if the two references being checked are to the same object. Otherwise it returns false. For
further information check the manual pages for StringBuffer
and Object.
About | Sitemap | Contact