wood burning stoves*
The moose likes Beginning Java and the fly likes StringBuffer Confusion????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer Confusion?????" Watch "StringBuffer Confusion?????" New topic
Author

StringBuffer Confusion?????

Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Hi,
i have written this program,
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
StringBuffer ss2=sb1;
System.out.println(ss2.equals(sb1));
System.out.println(sb2==ss2);

and got OUTPUT::
false
false
false
dar
true
false

Actually i want to undersatnd this in terms of object refernce. What actually takes place in memory. So that i can understand String buffer much better as i am always confused with it.
Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16
Looks like you have mixed up the equals method in the String and Object class a bit.

Since there are no equals method in the StringBuffer class it will use the equals method in the Object class. So a line like:

System.out.println(sb1.equals(sb2));

will of course be false since the equals method in the Object class just checks if its the objects equals, not the content of the StringBuffer.

That the line:
System.out.println(ss2.equals(sb1));
will be true isn't that strange since both pointers point to the same object.

Hope that helps
jaspal singh
Ranch Hand

Joined: Sep 15, 2005
Posts: 54
hi megha,

read this:
strings are immutable that once allocated memory cant b changed.
where as strinbuffer variable is muttable, i.e. once allocated memory its
contents can be changed.

if u get this concept,things are clearer then...


Teach me the art of forgetting, for I always remember wat I have forgotten.
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Hi u have written......

System.out.println(sb1.equals(sb2));

will of course be false since the equals method in the Object class just checks if its the objects equals, not the content of the StringBuffer.

Then why this statement evaluates false

System.out.println(sb1==sb2);

As both contain "Amit"
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Also tell me if i want that comparision of sb1 and sb2 should return true value.......... let me make myself more clearer...
I have created two instances of string Buffer and have stored same value in both say "Megha". as they are pointing to different memory locations, but i want to only compare the contains only and as both contain "megha" , it should return true value...
But when i write ....
SOP(sb1==sb2) it returns false....... how can i modify it so that i compare only the values and true value is returned
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Than you'll have to use
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Thanks for that......
Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16
Looks like you have gotten the answers you needed

Just wanted to reply on your question:
"Then why this statement evaluates false
System.out.println(sb1==sb2);"

Cause its the same comparison done as in:
System.out.println(sb1.equals(sb2));

i.e it just compares to see if its the same object reference and doesn't check that the content is the same.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
To clarify, the == operator results in true if and only if the two references point to the same object. By default, Object.equals() does the same thing. However, equals() can be overridden in any class to provide a more appropriate definition of equality. For example, String.equals() compares the contents of two String objects. However, StringBuffer does not override the equals() method, so it uses the default provide by Object.

I hope this helps you understand what is going on in your example.

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringBuffer Confusion?????
 
Similar Threads
string buffer
String and StringBuffer
string(buffer) comparision...
equals with stringbuffer
about equals