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
5
votes
2 answers

How to use replaceAll() method in StringBuffer?

I need to replace multiple words in a string buffer. So I am looking for a replaceAll method in StringBuffer. So do we have it in StringBuffer? String method: str2 = str1.replaceAll(regex, substr); // (This is String method, I need like this in…
5
votes
7 answers

What does synchronization means in terms of StringBuilder and StringBuffer classes?

I am quite confused with the term 'synchronized', I've got following from java documentation. A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is…
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64
5
votes
4 answers

String Vs Stringbuffer as HashMap key

I am trying to understand why String and Stringbuilder/StringBuffer are treated differently when used as Hashmap keys. Let me make my confusion clearer with the following illustrations: Example #1, using String: String s1 = new String("abc"); String…
ambar
  • 2,053
  • 6
  • 27
  • 32
5
votes
2 answers

Does the StringBuffer equals method compare content?

Possible Duplicate: Comparing StringBuffer content with equals StringBuffer s1= new StringBuffer("Test"); StringBuffer s2 = new StringBuffer("Test"); if(s1.equals(s2)) { System.out.println("True"); } else { System.out.println("False"); } Why…
rocker
  • 11,317
  • 7
  • 22
  • 12
5
votes
1 answer
4
votes
2 answers

Writing multiline JTextArea to txt file

so I Have a JTextArea for user input and then when they click a button it writes it to a text file, I have both setLineWrap and setWrapStyleWord set to true for the JTextArea. I would like to write to the text file the exact way it appears in the…
Beef
  • 1,413
  • 6
  • 21
  • 36
4
votes
2 answers

StringBuffer/StringBuilder size in java

All, Why is it suggested that the size of the StringBuffer/StringBuilder object should be initialized to a size of 2^{1...n}(Though usually it would be > 64). What would be the advantage/optimization would be achieved doing so?
name_masked
  • 9,544
  • 41
  • 118
  • 172
4
votes
5 answers

Does "+" use in String concatenation affect efficiency?

I have worked with String, StringBuilder and StringBuffer in java. I thought of this question, while I was thinking from efficiency point of view. Does "+" use in String concatenation affect efficiency?
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
4
votes
1 answer

Performance of StringBuilder.append

It's recommended (PMD rule AppendCharacterWithChar) to use StringBuilder.append(char) instead of StringBuilder.append(String). I agree with that. But if I want to append a (short) string like "='" or "
Frank M.
  • 997
  • 1
  • 10
  • 19
4
votes
6 answers

Java : Clearing StringBuffer contents

All, I was wondering if clearing a StringBuffer contents using the setLength(0) would make sense. i.e. Is it better to do : while () { stringBufferVariable = new StringBuffer(128); stringBufferVariable.append() …
name_masked
  • 9,544
  • 41
  • 118
  • 172
4
votes
2 answers

Java StringBuffer append allocation

When using StringBuffer in java, I'm wondering how the append function is implemented when it needs to reallocate space. For example, if I append a string longer than the currently allocated space, how does it manage this in the details of the…
NullPointer0x00
  • 1,808
  • 3
  • 18
  • 20
4
votes
4 answers

How to remove last character of string buffer in java?

I have following code, I wanted to remove last appended character from StringBuffer: StringBuffer Att = new StringBuffer(); BigDecimal qty = m_line.getMovementQty(); int MMqty = qty.intValue(); for (int i = 0; i < MMqty;i++){ …
Lina
  • 305
  • 1
  • 6
  • 15
4
votes
2 answers

Replace StringBuffer/StringBuilder by String

Why does NetBeans suggest that I replace StringBuffer / StringBuilder by String? It shows me a warning message when I write: StringBuilder demo = new StringBuilder("Hello");
Zehafta Tekea
  • 41
  • 1
  • 5
4
votes
6 answers

String vs StringBuffer returned value

Can you please explain why in the following code String and StringBuffer are treated differently and when value is appended in StringBuffer but not in String. public class MyClass { public static void main(String args[]) { String…
Rohan Kushwaha
  • 738
  • 6
  • 18
4
votes
3 answers

remove certain lines from a StringBuffer

A legacy app program has a huge String Buffer (size sometimes upto an Mb) and it is processed sequentially for modifying the contents. I have to implement a change wherein I need to update the string buffer to remove some lines starting with certain…
jai
  • 21,519
  • 31
  • 89
  • 120