Questions tagged [stringbuffer]

StringBuffer in Java is used as a thread-safe, mutable sequence of characters, replaced by StringBuilder in Java 5.0+

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

In Java 5.0, it was replaced by StringBuilder as this was faster, and there was almost not good use case for a thread safe StringBuffer and classes which attempted to use it as such ended up not being thread safe anyway. e.g. SimpleDateFormat.

The Javadoc for StringBuffer notes

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

485 questions
1732
votes
33 answers

Difference between StringBuilder and StringBuffer

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?
blacktiger
  • 18,957
  • 5
  • 20
  • 11
184
votes
27 answers

Create a string with n characters

Is there a way in Java to create a string with a specified number of a specified character? In my case, I would need to create a string with ten spaces. My current code is: final StringBuffer outputBuffer = new StringBuffer(length); for (int i = 0;…
C. Ross
  • 31,137
  • 42
  • 147
  • 238
62
votes
15 answers

What is the difference between String and StringBuffer in Java?

What is the difference between String and StringBuffer in Java? Is there a maximum size for String?
Praveen
  • 90,477
  • 74
  • 177
  • 219
58
votes
10 answers

What is difference between mutable and immutable String in java

As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new…
Raghu
  • 1,324
  • 7
  • 24
  • 47
53
votes
5 answers

StringBuffer is obsolete?

In the book "Effective Java", Josh Bloch says that StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder' . But in my experience, I've still seen widespread use of the StringBuffer class.…
Varun Achar
  • 14,781
  • 7
  • 57
  • 74
38
votes
11 answers

Converting Char Array to List in Java

Can anyone help me and tell how to convert a char array to a list and vice versa. I am trying to write a program in which users enters a string (e.g "Mike is good") and in the output, each whitespace is replaced by "%20" (I.e "Mike%20is%20good").…
bourne
  • 1,083
  • 4
  • 14
  • 27
34
votes
2 answers

Convert a StringBuffer to a byte Array in Java

How might I, in Java, convert a StringBuffer to a byte array?
Sanat Pandey
  • 4,081
  • 17
  • 75
  • 132
34
votes
10 answers

What is the complexity of this simple piece of code?

I'm pasting this text from an ebook I have. It says the complexity if O(n2) and also gives an explanation for it, but I fail to see how. Question: What is the running time of this code? public String makeSentence(String[] words) { StringBuffer…
Someone
  • 341
  • 1
  • 3
  • 3
28
votes
4 answers

When was Javac StringBuilder/StringBuffer optimization introduced?

I know that Javac compiler is able to transform String concatenation + using StringBuilder/StringBuffer, and I'm curious to know starting from which version this change was introduced? I'm using this sample code: public class Main { public static…
Nicolas C
  • 1,584
  • 2
  • 17
  • 33
26
votes
5 answers

Java StringBuilder(StringBuffer)'s ensureCapacity(): Why is it doubled and incremented by 2?

I have searched about this, but I couldn't find why StringBuilder's ensureCapacity() method won't lengthen the old capacity by just doubling but also adding two. So, when default capacity of 16 is full, next lengthened value will be 34 unless whole…
Philip Paek
  • 271
  • 2
  • 6
25
votes
2 answers

Warning in StringBuffer and StringBuilder

I have a StringBuffer initialized outside for loop and inside for loop I am concatenating some strings. I am getting the warning 'StringBuffer stringBuffer' may be declared as 'StringBuilder' and string concatenation as argument to…
Abish R
  • 1,537
  • 3
  • 18
  • 36
21
votes
6 answers

How to get address of a Java Object?

Is there a way to get address of a Java object? Where the question comes from?: At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using…
Iguramu
  • 2,080
  • 5
  • 26
  • 30
18
votes
3 answers

How to convert StringBuffer to InputStream in Java ME?

I'm new in Java and learning Java ME development. I got stuck in this conversion. Please help me to convert StringBuffer to InputStream. Thanks!
masiboo
  • 4,537
  • 9
  • 75
  • 136
17
votes
8 answers

How to Reassign value of StringBuffer?

How can we re assign the value of a StringBuffer or StringBuilder Variable? StringBuffer sb=new StringBuffer("teststr"); Now i have to change the value of sb to "testString" without emptying the contents. I am looking at a method which can do this…
Biju CD
  • 4,999
  • 11
  • 34
  • 55
17
votes
4 answers

deleteCharAt or setLength, which way is better to remove last char from a StringBuilder/StringBuffer

In many cases, we need to delete the last char of a StringBuilder/StringBuffer. For example, given a int[]{1,2,3}, to implement a String toString(int[] a) method, contacting each elements with a comma separator. The output should be 1,2,3, no…
Weibo Li
  • 3,565
  • 3
  • 24
  • 36
1
2 3
32 33