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
3
votes
1 answer

Adding column to CSV file with StringBuffer values (Java)

Assume I have a StringBuffer with values "1 \n 2 \n 3 \n...etc" where \n is a line break. How would I add these values to an existing CSV file as a column using Java? Specifically, this would be the last column. For example, let's say I have a CSV…
dunce1
  • 323
  • 5
  • 21
3
votes
4 answers

Coding for the first time (Java hangman game)- going from console-based to GUI

I am completely new to programming and I have to complete some tasks in Java (I'm using Eclipse). This one is a hangman game. My code is below. I have written a console-based program that basically works (counting lives instead of drawing a hangman…
NewGirl
  • 51
  • 1
  • 1
  • 5
3
votes
0 answers

Using a circular/ring buffer in Android

I have been searching online for some time and I cant find any information about circular / ring buffers for Android. I tried to use the ByteBuffer but it is not doing what I want. I would like o have a simple circular buffer, that have start and…
Vasco Baptista
  • 653
  • 2
  • 8
  • 15
3
votes
2 answers

Concatenation of constant strings VS StringBuffer

What arguments can You give for the use one or another variant that is better, faster, more correct. First variant: StringBuffer sql = new StringBuffer("SELECT DISTINCT f.ID ") .append("FROM FIRST_TABLE F ") .append("LEFT JOIN…
old
  • 33
  • 3
3
votes
3 answers

Are Spring Controllers Thread-Safe?

I am writing a private utility method in a Spring Controller. I need to use either StringBuffer or StringBuilder. The former is thread-safe, the latter is not, but the latter is much faster. Basically, it looks like this: private String…
EdgeCase
  • 4,719
  • 16
  • 45
  • 73
2
votes
5 answers

Java Explain: why String immutable make StringBuffer MORE efficient

I have read in a Java book that says: Because a String is immutable, using StringBuffer is more efficient. I understand that String instances are immutable. I also understand that StringBuffer makes processing Strings more effcient than…
hqt
  • 29,632
  • 51
  • 171
  • 250
2
votes
2 answers

StringBuilder vs. StringBuffer - mnemonic wanted

I know the technical differences between StringBuffer and StringBuilder. But if I don't use them for half a year or so, I just forget which one is synchronized and which one is not. I always have lookup the first sentence of the JavaDoc. So: Is…
A.H.
  • 63,967
  • 15
  • 92
  • 126
2
votes
2 answers

How to force use of StringBuffer instead of StringBuilder by default in javac?

I am trying to compile the CLDC with kvm on linux, I get an error cannot access java.lang.StringBuilder class file for java.lang.StringBuilder not found at the statement: return getClass().getName() + "@" + Integer.toHexString(hashCode()); I…
adarshaj
  • 1,224
  • 1
  • 11
  • 24
2
votes
3 answers

Why am I getting an incompatible types from the java compiler?

I'm a Java newbie, but I am an experienced programmer (Assembler, COBOL, C/C++, REXX) I can't understand why the compiler is giving me the following: [loading java\lang\Throwable.class(java\lang:Throwable.class)] src\LU62XnsCvr.java:265:…
Guy Rich
  • 451
  • 3
  • 8
  • 18
2
votes
3 answers

What's the equivalent C# method to this basic Java method?

Really simple question here (more to confirm my thoughts, than anything)... Java method: StringBuffer.Delete(start,end); Java code: sb.delete(sb.length()-2, sb.length()); C# (not sure if this is right): StringBuilder sb = new…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
2
votes
1 answer

Using same stream object to write to filestream or stringstream

I am trying to use an ostream object to write to either to a filestream of stringstream based user input (similar to fmemopen in Linux). I realized that ostream doesnt take stringstream or fstream objects but instead takes stringbug or filebuf. I…
Trancey
  • 699
  • 1
  • 8
  • 18
2
votes
4 answers

Why String, StringBuffer and StringBuilder classes use byte array instead of character array to store characters of a string?

A byte cannot accommodate unicodes of characters from various languages of the world. So using a byte array we cannot have a string of different languages. Why these classes use byte array instead of character array ? UPDATE: class First { …
my name is GYAN
  • 1,269
  • 13
  • 27
2
votes
0 answers

String Buffer thread safety

StringBuffer is known to be thread-safe in Java which means it provides synchronized access to multiple threads.. But when I run the below code, it does not run same as in synchronization block.. What am I missing here? class Hackerearth{ static…
Akshit Gupta
  • 109
  • 1
  • 8
2
votes
1 answer

Why does the StringBuffer class uses an Array as it's underlying data structure instead of a LinkedList?

The StringBuffer/StringBuilder classes in Java are primarily used to modify String values without having to initialize a new String object everytime. Is there a specific reason, it doesn't use a LinkedList instead of an Char Array as it's underlying…
Sam...
  • 71
  • 6
2
votes
1 answer

StringBuffer in flutter doesn't working as expected

I am currently just started a Flutter project and is found quite frustrated when playing around with the StringBuffer class, I am having the below codes which format and apply the url to my class; Connector._baseUri = baseUri; if…
Zay Lau
  • 1,856
  • 1
  • 10
  • 17