PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

StringBuffer in Java

← All stacks

Theory

79/270

StringBuffer in Java

StringBuffer is a mutable text builder. append, insert, reverse change the same object. Methods are synchronized — safer if two threads share it, a bit slower.

In a single-thread student program, StringBuilder is usually the better pick. Use StringBuffer when the buffer is shared across threads.

Don’t build a long string with + in a loop. Use append.

Let's take this on the board with one tiny Main class — no extra files. StringBuffer in Java — same append idea as Builder, but synchronized. Start from main, go line by line, and stop at each print. That output is the proof for StringBuffer.

Exam tip

Mutable + synchronized. Prefer StringBuilder unless two threads share it.

Example

// StringBuffer demo
public class Main {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hi");
    sb.append(" Java");
    System.out.println(sb);
  }
}

StringBuffer in Java — same append idea as Builder, but synchronized.

Short notes

  • DefStringBuffer = mutable text. append changes the same object.
  • RuleMethods are synchronized (thread-safe, slower).
  • UseShared across threads. One thread → StringBuilder.
  • Trap+ in a loop instead of append.

Questions

1

Is StringBuffer immutable?

2

Why synchronized?

Previous← Java String MethodsNextStringBuilder in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.