• 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

StringBuffer Question.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer Question.
I am new to Java programming, please help me with this.
I wanted to check if two String buffers have same values.
Why should it return unmatch in these cases?

class Sb_Test {
public static void main(String args[]) {
StringBuffer sb1,sb2;
sb1 = new StringBuffer("Raj");
sb2 = new StringBuffer("Raj");
if (sb1.equals(sb2))
{ System.out.println("String buffers match"); }
else
{ System.out.println("String buffers unmatch"); }
if (sb1 == sb2)
{ System.out.println("String buffers match"); }
else
{ System.out.println("String buffers unmatch"); }
/* if (sb1.compareTo(sb2))
{ System.out.println("String buffers match"); }
else
{ System.out.println("String buffers unmatch"); }
doesn't compile */
}
}

Result :
String buffers unmatch
String buffers unmatch
.equals should work, why it doesn't work with Stringbuffers , but works with strings
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u r new to Java, then please download JLS ( Java language specification ) from sun site and read it once. Then read the resources from www.javaranch.com/maha Most of ur simple, major doubts will be cleared.
In the Question u hv asked the right answer is strings doesnt match. Also put a search on various topics in this same discussion session. This kinda qs are already discussed sometime back
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,
If you see in java API, StringBuffer class doesn't override the equals() method of Object class. Hence it tries to compare whether they refer to the same object not the contents of string. It is defined in Object class.
But String class overrides the equals() method and it compares the contents.
Regards
Rashmi.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, make sure you understand this one right. There can be a lot of tricky questions asked in the exam based on this concept. Imagine various combinations of equals() and workout the results for example try the following -
StringBufferObject.equals( StringObject )
StringObject.equals( StringBufferObject )
StringObject.eqeals( StringBufferObject ) etc.
Just my two cents worth ..
Ajith
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raj Dev,
The method equals is defined in Object thus
<pre>
public boolean equals(Object obj)
{
return (this == obj);
}
This method is overridden in String thus
public boolean equals(Object anObject)
{
if (this == anObject) return true;
if (anObject instanceof String)
{
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count)
{
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0)
{
if (v1[i++] != v2[j++]) return false;
}
return true;
}
}
return false;
}
</pre>
The first test is to see if both objects are the same. If true
the function returns here with a true value. The next test is to see if the passed object is an instance of String. The next comparison is the integercount which holds the number of characters in the string if they are not of equal length the strings cannot be equal. If all these conditions are met then the elements of the char array are compared one by one. The data member value is a char array. At the first instance of in-equality the loop is terminated and the function returns with a false value. If everything goes fine until all the characters are compared the function returns true.
The StringBuffer class does not override this method so the
original method defined in Object is executed which is a comparison of object references. It returns true if both references are referring to the same object.
It helps to occassionally take a peek into the foundation classes. It gives you an insight into how the big boys do it.
Not only with java, with any other language you use. If you want to see REAL big boys in action take a look at Borland's Object Windows Library.
Rgds
Sahir


[This message has been edited by Sahir Shah (edited December 07, 2000).]
 
Raj Dev
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sahir, Ajith , Rashmi, Anboo for the reply.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the post of Ajith,
I would like to add just for information that a string and stringbuffer with similar contents always return false in the equals test. (Courtesty: Simon Roberts)
Shree
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi raj,
I think it is the concept of similar contents.
In case of string equals & == return true if the object & contents are same.
But in case of StringBuffer == would never return true even if the contents are same.
Why?
Because unlike Strings,Stringbuffer contains 16 extra bits of storage when instantiated.
for ex.
StringBuffer s=new StringBuffer("hello");
will assign a memory of 21(5 for hello & rest as addiional
as it can be appended).so when you compare two StringBuffers
with same content then in addition to "hello" rest 16 memory bits are also compared which are null .
and since null==null returns false
THEREFORE HENCE.
I hope I am not wrong.If i am kindly tell me so .
bye
 
Ranch Hand
Posts: 103
  • 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 has anything to do with StringBuffer using extra storage. One of the previous posts in this discussion hit it on the head (I think) when they stated that the problem is that StringBuffer does not override the equals method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic