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

How can I see if a Class is built into Java?

I'm doing a Java project at the moment and feel that StringBuffer would help a lot in performing this particular task, where I am 'building' a string representation of a Matrix: /** * Returns a String representation of the Matrix using the getIJ…
-2
votes
3 answers

String object creation

I'm new to Java, and have a question related to the creation of strings. Case 1: String a = "hello"; String b = "world"; a = a + b; System.out.println(a); Case 2: String a; String a = "hello"; a = new String("world"); System.out.println(a); I…
Thiyagarajan
  • 1,271
  • 1
  • 14
  • 19
-2
votes
2 answers

leakage in StringBuffer.toString() casting issue

How to fix the issue of data/memory leakage in StringBuffer.toString() casting in Java. I have found some tips like using a SAX parser for XML but need some more help. Does anyone have any alternative suggestions. Thanks in…
R.K.R
  • 132
  • 4
  • 18
-2
votes
4 answers

StringBuffer Array to String Array - Java

I need to convert a StringBuffer Array to String Array for sorting. Is there any method in Java? Below is my code and i am trying to sort the values(input1) in alphabetical order that contains the first letter as UpperCase and ignoring other…
Sankar V
  • 4,794
  • 3
  • 38
  • 56
-3
votes
1 answer

Why program is not printing true in second and third line?

Source Code: public class TestSB{ public static void main(String args[]){ String s1="Arnold"; StringBuffer sb1=new StringBuffer("Arnold"); StringBuffer sb2=new StringBuffer("Arnold"); …
-3
votes
1 answer

How can I replace specific part of String with another string in java?

I have been trying to replace specific part of String(array of space characters) with another String. for Example String string1 = " "; String string2 = "hello"; I want to replace the first 5 characters in string1 with "hello". Can anyone…
kishoref22
  • 56
  • 1
  • 12
-3
votes
2 answers

Characters not getting converted to lower case , eventhough the string has all lower case letters

Trying to solve a leetcode problem of "valid palindrome" below is the code . when I convert the stringbuffer to string and to lower case the string is getting printed as all lower case letters, but when I access it for comparisons using s.charAt(i),…
-3
votes
1 answer

How to pass values of StringBuffer to different Activities using Intent

Code of StringBuffer data: StringBuffer finalbufferdata = new StringBuffer(); for (int i = 0; i < parentarray.length(); i++) { JSONObject finalobj = parentarray.getJSONObject(i); String id = finalobj.getString("student_id"); String name…
-3
votes
1 answer

Differences between StringBuffer and StringBuilder

Why StringBuffer is thread safe and why StringBuilder is not. Why the StringBuffer is working best in multi-threading and StringBuilder is in single-threading. I saw there are methods which are common for both StringBuilder and StringBuffer, then…
Dhivakar
  • 2,081
  • 5
  • 15
  • 18
-4
votes
1 answer

How to fix StringBuffer bug

i got the message from compiler which is The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files I'm using drjava and Eclipse Compiler 0.A48.How to fix it ? it's my following code: private…
Ybakman
  • 1
  • 2
-4
votes
1 answer

How StringBuffer delete works in java

In java, the StringBuffer function will work like this str="hello" like it is specified the str.delete(1,3). I wanted to know how it works, like how it takes the position class StringBufferExample4 { public static void main(String args[]) { …
devd
  • 7
  • 3
-4
votes
1 answer

StringBuffer capacity()

public static void main(String args[]){ StringBuilder sb=new StringBuilder(); System.out.println(sb.capacity()); sb.append("abcabcabcabcabcabcab"); System.out.println(sb.length()); System.out.println(sb.capacity()); …
Ruchi Gupta
  • 89
  • 1
  • 9
-4
votes
2 answers

Anagram Program to Compare two Strings

I am creating an anagram program which compares two strings and am unsure how to go about creating a boolean that returns true if a character appears in both words. My code is as follows: StringBuffer strbuff1 = new StringBuffer(""); private…
CsStudent
  • 1
  • 2
-4
votes
2 answers

How to generate a char randomly between 'X' and 'O'?

public static String generate(){ StringBuffer stringBuffer = new StringBuffer(); for(int i = 0; i < TicTacToeUtil.NUM_OF_SPACES + 1; i++){ for(int j = 0; j < TicTacToeUtil.NUM_OF_SPACES + 1; j++) { …
Rob Ye
  • 63
  • 1
  • 7
-4
votes
1 answer

Is there a way to bring Strings together in java apart from StringBuffer and toString?

String Fehlerspeicher_ist = "Fehlerspeicher"; String Fehlerspeicher_soll = "20_Fehlerspeicher"; String lines[] = temp.split("\n"); StringBuffer result = new StringBuffer(); for (int i=0; i< lines.length; i++) { if…
1 2 3
32
33