This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuilder vs StringBuffer  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please say me difference between stringbuffer and stringbuilder...when to use each.and if possible please provide me website where i can see examples of stringbuffer and stringbuilder and practise so that i can be comfortable in using them...thanks
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation tells you the difference: it says about StringBuilder

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case).



So in simpler terms, StringBuilder is identical to StringBuffer except that its methods aren't synchronized.
 
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You usually use them as local variables, so synchronisation may be unnecessary and StringBuilder is usually slightly quicker. Recent JVMs may optimise that difference away.
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone for your reply..i surfed on net and im very much comfortable with StringBuilder and strings...i have one doubt

StringBuilder sb1 = new StringBuilder();

char[] name = {'a','k','s','h','a','y'};

sb1.append(name,0,5);//aksha.....why not "akshay".. at 0 position it is 'a' and at 5 position it is 'y'...

if in this case output is ava..

StringBuilder sb1 = new StringBuilder();
char[] name = {'J', 'a', 'v', 'a', '7'};
sb1.append(name, 1, 3);
System.out.println(sb1);//ava....why not "av"
 
Paul Clapham
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for that method says that 5 characters and 3 characters will be appended by your two examples respectively (take a minute and check it out). Since the StringBuilder is initially empty (as far as I can tell), it would follow that the results would contain 5 characters and 3 characters respectively, no?
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:The documentation for that method says that 5 characters and 3 characters will be appended by your two examples respectively (take a minute and check it out). Since the StringBuilder is initially empty (as far as I can tell), it would follow that the results would contain 5 characters and 3 characters respectively, no?

...ok thanks i got it...5 and 3 are length ..
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akshay Rawal wrote:

Paul Clapham wrote:The documentation for that method says that 5 characters and 3 characters will be appended by your two examples respectively (take a minute and check it out). Since the StringBuilder is initially empty (as far as I can tell), it would follow that the results would contain 5 characters and 3 characters respectively, no?

...ok thanks i got it...5 and 3 are length ..

...this string is eating enough of my brain..one more confusion..

String s1 = new String("gg");
String s2 = new String("gg");

s1.equals(s2)//true...i understood....now


StringBuffer sb1 = new StringBuffer("hi");
StringBuffer sb2 = new StringBuffer("hi");
StringBuffer sb3 = new StringBuffer(sb2);



StringBuffer sb4 = sb3 ;


System.out.println(sb1.equals(sb2));//false...why...are they comparing reference or string value
System.out.println(sb4.equals(sb3));//true...why...confused

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:So in simpler terms, StringBuilder is identical to StringBuffer except that its methods aren't synchronized.


And when is Java going to incorporate both of them into a StringHandler interface? That would make the whole business a whole lot easier, and not force us into having to "choose" between implementations (or at least make the choice a lot simpler to change, if we need to).

It's amazing to me that it didn't occur to anyone when they were implementing a class with 50 methods in it.

Winston
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder
- not synchronized
- cannot be used in concurrent environment
- faster than StringBuffer

StringBuffer has opposite features to what are above for StringBuilder
 
Campbell Ritchie
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the differences in behaviour of equals by going to the API documentation. Look where the equals method is described. Now repeat the exercise for StringBuilder. See the difference?

The equals method is not necessarily implemented for classes which are designed to change rapidly. They obviously thought nobody would put a StringBuilder into a List and see whether another StringBuilder with identical contents existed in that List.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer class enables you to represent and manipulate sequence of characters. Unlike String class, StringBuffer class is mutable. That is, you can change the contents of a StringBuffer object. StringBuilder is a mutable sequence of characters and this class is compatible with StringBuffer, but with no guarantee of synchronization. For more details and practice programs please visit http://java.meritcampus.com/t/234/String-buffer-basics http://java.meritcampus.com/t/259/Stringbuilder
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

StringBuilder
- cannot be used in concurrent environment


That's not correct. You just need to take care to handle it in a thread-safe manner.
 
Campbell Ritchie
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch PremSai Botta
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!