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
4
votes
3 answers

Each call to StringBuffer#toString and StrinBuilder#toString returns new instance or instance from string pool?

My question is if am using a StringBuffer(or StringBuilder) and if I call toString method on the instance multiple times. Will StringBuffer return new instance of String each time or it returns String from String pool? ( assuming I have not made…
Jeevan
  • 8,532
  • 14
  • 49
  • 67
4
votes
4 answers

Need clarification on final StringBuffer object

In general if a variable is declared final, we cant override the value of that variable but this doesn't hold good when we use string buffer. Can someone let me know why? The below code works!!!!!! public static void main(String args[]) { …
Learner
  • 2,303
  • 9
  • 46
  • 81
4
votes
8 answers

remove html tag in android

I have follows below XML feed:

Touch, tap, flip, slide! You don't just read Books, you experience it.

Here I have to display the description like Touch,tap,flip,slide! You don 39.just read the Books, you…
user2098063
  • 85
  • 1
  • 1
  • 7
4
votes
7 answers

How is StringBuffer used with .append

I'm doing a computer science project and I need to incorporate StringBuffers. I need help with what .append does and what concatenate means. Somebody told me I can show who is the winner (of my game) by using .append with StringBuffer. public…
user1965081
  • 55
  • 1
  • 2
  • 8
4
votes
2 answers

String Buffer To String Conversion Exception?

Hi Check the below code ArrayList x=new ArrayList(); ArrayListy=x; ArrayListz=x; y.add("Strings"); System.out.println(z.get(0).toString()); Am getting Class Cast Exception at System.out.println …
Anand K Nair
  • 597
  • 1
  • 6
  • 18
3
votes
5 answers

Why does the capacity change to 112 in the following example?

In the following code... StringBuffer buf = new StringBuffer("Is is a far, far better thing that i do"); System.out.println("buf = "+ buf); System.out.println("buf.length() = " + buf.length()); System.out.println("buf.capacity() = " +…
CP3O
  • 429
  • 1
  • 6
  • 21
3
votes
3 answers

"StringBuffer is synchronized (or thread-safe) and StringBuilder is not", why does this make StringBuffer methods slower?

After reading this - What does 'synchronized' mean? I was still unable to understand why StringBuffer would be slower than StringBuilder in a thread-safe environment. What extra time-consuming work does StringBuffer have to do that makes it slower?
3
votes
2 answers

How hash code is generated for StringBuffer object in java?

I am studying String in java and while studying I came to know that hash code is generated for every string. I want to know how hash code is generated in the case of StringBuffer.
Jasmeet Singh
  • 133
  • 1
  • 5
3
votes
1 answer

What prevents from making Cloneable a mutable object like StringBuilder?

It is a bad idea to make an immutable object Cloneable. This is why String is not Cloneable. Immutable BigInteger and BigDecimal are also not Cloneable. But mutable StringBuilder and StringBuffer cannot be cloned! What is the reason behind that…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
3
votes
1 answer

field 'int count' in Java AbstracStringBuilder class is set in/by which method?

I understand StringBuffer uses the fields 'value' and 'count' which StringBuffer inherits from AbstractStringBuilder. The constructor StringBuffer() for example calls on AbstractStringBuilder(int capacity) to create a 16-Bit-array using super(16).…
3
votes
2 answers

StringTokenizer vs split() in a program

I have to capitalize the first character of every word in a sentence. First character of the first word is in uppercase, and the sentence is to be terminated by a full stop or a question mark only. Then I have to count all the vowels and the…
Divyanshu Varma
  • 122
  • 3
  • 17
3
votes
2 answers

String vs StringBuffer. Tip of IDEA

Intellij Idea offers to replace the following: StringBuffer sb = new StringBuffer(); sb.append("Name=").append(name).append(", name2=").append(name2).append(", list=").append(list); return sb.toString(); To: return "name=" + name + ", name2=" +…
DmitMedv
  • 980
  • 3
  • 11
  • 22
3
votes
4 answers

How does ensureCapacity work in Java?

StringBuffer buff1 = new StringBuffer("tuts point"); System.out.println("Old Capacity of buff1 = " + buff1.capacity()); buff1.ensureCapacity(28); System.out.println("New Capacity of buff1 = " + buff1.capacity()); StringBuffer buff2 = new…
Gauri Shankar
  • 91
  • 3
  • 9
3
votes
1 answer

Java regex on a long string(String buffer)

I have 2 questions actually. 1st theoretical- If I have a long text in a StringBuffer and I want to find a regular expression inside of it- do I have to worry that the StringBuffer will take chunks of the StringBuffer look for the regex inside of…
user3142434
  • 305
  • 3
  • 15
3
votes
2 answers

StringBuffer class and Chinese character encoding

I have written a method to return a string containing Chinese characters. public printChineseMenu(){ StringBuffer buffer; buffer.append(chinese string returned from DB); //chinese characters appear in SQL System.out.println(buffer); …
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107