Results 1 to 10 of 10
Thread: StringBuffer
- 03-01-2011, 08:52 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
StringBuffer
package mutable;
public class Expt
{
public static void main(String[] args)}
{
StringBuffer sb = new StringBuffer();}
sb.append("abc");
sb.append("xyz");
StringBuffer sb1 = sb.append("123");
System.out.println("sb1->"+sb1);
System.out.println("sb->"+sb);
System.out.println(sb==sb1);
System.out.println(""+sb==""+sb1);
output
sb1->abcxyz123
sb->abcxyz123
true
false
what makes the difference between the below to statements
System.out.println(sb==sb1);
System.out.println(""+sb==""+sb1);
why (" "+sb==" "+sb1) is false
Thanks in advance
DayaLast edited by Dayanand; 03-01-2011 at 08:55 AM. Reason: mistakes
- 03-01-2011, 08:56 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You are comparing references not values with == and in the second one new objects are being creted due to the string concatenation and these new objects have different reference values even though the actual string values are the same.
IOW do not use == to compare objects (of any kind) unless you wish to see if those variables reference the exact same object. If you wish to compare the values of the objects behind those references use the equals method.
- 03-01-2011, 09:13 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
thanks for your valuable solution
one more doubt
but i think no new objects will be created in case of StringBuffer,has StringBuffer provides Mutable objects
can u please help me out in this case.?Last edited by Dayanand; 03-01-2011 at 09:14 AM. Reason: clearity
- 03-01-2011, 09:17 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Doesn't matter. String concatenation (which is what that + causes in that form) produces Strings not StringBuffers.
Edit: And, it doesn't really matter what that + produces, nor does it matter that mutable objects were fed into it, it produces new objects.Last edited by masijade; 03-01-2011 at 09:22 AM.
- 03-01-2011, 09:22 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
sorry Sir,for asking u again and again.!!!!!!!!!!
System.out.println(""+sb==""+sb1);
""+sb will provide one string
""+sb1 will provide one string
but at last sb & sb1 strings will have same content, but why it results in false when sb==sb1 is checked
- 03-01-2011, 09:32 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because they are different objects. I.E. they are represented by different structures on the heap even though those structures contain the same value they are still different structures and so have different "addresses" and == compares these "addresses" not the values of the structures behind those "addresses".
- 03-01-2011, 09:43 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 03-01-2011, 09:44 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 03-01-2011, 09:55 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because you are comparing sb to ("" + sb1) and prepending "" to that result, you are not comparing ("" + sb) to ("" + sb1)
Java Code:(""+sb).equals(""+sb1)
- 03-01-2011, 10:04 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
Similar Threads
-
How to use a StringBuffer?
By Hallowed in forum New To JavaReplies: 5Last Post: 02-18-2011, 05:38 AM -
How to do this with StringBuffer???
By mlibot in forum New To JavaReplies: 2Last Post: 03-24-2010, 12:40 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 06:03 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum New To JavaReplies: 1Last Post: 03-03-2009, 05:27 AM -
Should I use StringBuffer?
By GenkiSudo in forum New To JavaReplies: 7Last Post: 09-21-2008, 12:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks