Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsStrings, Keywords, Exceptions & Serialization
✓ FreeBeginner· 6 min read

Strings, String Pool, StringBuilder & StringBuffer — Interview Questions

How the string pool works, literals vs new String(), when interning hurts, String vs StringBuilder vs StringBuffer, and exactly why String is immutable.

Published September 25, 2026


How to use this lesson

String questions are practically guaranteed in a Java interview, and they often come with a "what does this print?" snippet. Know the pool rules precisely, and be ready to say why String is immutable in at least three ways.

Q1. What is the string pool?

Short answer: The string pool (the "intern pool") is a JVM-managed table of unique String instances. String literals, and strings you explicitly intern(), are stored there once and reused. Two literals with the same text share a single object.

String a = "order";              // goes to (or is found in) the pool
String b = "order";              // same pooled object
String c = new String("order");  // a NEW object on the heap, outside the pool
String d = c.intern();           // returns the pooled "order"

System.out.println(a == b);      // true
System.out.println(a == c);      // false
System.out.println(a == d);      // true

Key points to cover:

  • Since Java 7, the pool lives in the regular heap, not PermGen, so pooled strings can be garbage-collected.
  • Only literals, compile-time constants and interned strings go into the pool. Strings built at runtime (concatenation of variables, substring, reading input) don't, unless you intern them.
  • Compile-time constants are folded: "ord" + "er" is the same pooled object as "order". But var + "er" creates a new string at runtime.

Learn it in depth → String Manipulation

Q2. When is the string pool (interning) not beneficial?

Short answer: When strings are mostly unique, or short-lived. Interning costs a hash lookup and keeps an entry in the pool table, so doing it on unique values (request IDs, UUIDs, user input) wastes CPU and memory, and saves nothing.

Key points to cover:

  • Interning pays off for a small set of values that repeat a lot, for example country codes or status values parsed from millions of records.
  • For deduplication at scale, G1's -XX:+UseStringDeduplication shares the underlying character arrays of equal strings automatically, without changing identity semantics.
  • Never write new String("literal"). It creates a pointless extra object.

Q3. What's the difference between String and StringBuffer?

Short answer: String is immutable. Every "modification" creates a new object. StringBuffer is a mutable sequence of characters that you change in place. Its methods are synchronized, which makes it thread-safe but slower.

String s = "a";
s = s + "b";                       // creates a new String; the old "a" is unchanged

StringBuffer sb = new StringBuffer("a");
sb.append("b");                    // modifies the same object

Learn it in depth → String Manipulation

Q4. How is StringBuilder different from StringBuffer, and when should you use each?

Short answer: They have the same API. StringBuilder (Java 5) is not synchronized, so it's faster, and it's the right choice almost always, because string building usually happens inside one method on one thread. StringBuffer is a legacy class, kept for code that genuinely shares one buffer between threads.

StringStringBuilderStringBuffer
MutableNoYesYes
Thread-safeYes (immutable)NoYes (synchronised)
Speed for repeated editsSlow (new objects)FastestSlower (locking)
Use forValues, keys, constantsBuilding strings in loopsRare legacy/shared cases
StringBuilder csv = new StringBuilder();
for (Order o : orders) {
    csv.append(o.id()).append(',').append(o.total()).append('\n');   // one buffer, no garbage
}

Key points to cover:

  • The compiler already optimises simple a + b + c expressions (using invokedynamic/StringConcatFactory since Java 9). Use StringBuilder explicitly for loops, where += creates a new string on every iteration.

Q5. Give a scenario where StringBuffer is better than String.

Short answer: When one character buffer must be modified by several threads. For example, several worker threads appending fragments to a shared diagnostic report, where each append must be atomic.

Key points to cover:

  • Even here, a better design is usually for each thread to build its own StringBuilder, and then combine the results. Or use a thread-safe logger or queue. Shared mutable buffers are a concurrency smell.
  • Compared with String: any scenario with many modifications (in loops) favours a mutable builder over repeated concatenation.

Q6. What's the difference between a string literal and a string object?

Short answer: A literal ("java") is pooled: equal literals share one instance. new String("java") always creates a new, separate object, even though an equal string already exists in the pool.

Key points to cover:

  • A classic question is "how many objects does new String("java") create?" The answer: up to two. The literal "java" is pooled (if it isn't already there, when the class is loaded), plus the new heap object.
  • Always compare contents with equals(), never with ==.

Q7. Why is String immutable in Java?

Short answer: For security, safe sharing (pooling), thread safety and hash caching.

Key points to cover:

  • String pool: literals are shared. If one reference could change the text, every other holder of that literal would see the change.
  • Security: strings are used for class names, file paths, URLs, database credentials and permission checks. A value that has been validated can't be changed afterwards.
  • Thread safety: strings can be shared between threads with no synchronisation.
  • Hash caching: hashCode() is computed once and cached, which makes strings fast and reliable HashMap keys.
  • Class loading: the JVM relies on class names not changing while a class is being loaded.

Common trap: saying "String is immutable because it's final". final on the class only prevents subclassing, which protects the immutability. The immutability itself comes from a private internal array that's never modified or exposed.

Follow-up questions this topic invites — and their answers

Q: How is a String stored internally in modern Java? A: Since Java 9 (compact strings), a String holds a byte[] plus a coder flag. Latin-1 text uses one byte per character, and anything else uses UTF-16. This roughly halves memory for mostly-ASCII text.

Q: Why is char[] preferred over String for passwords? A: A String can't be wiped, and it may linger in memory, in the pool, or in heap dumps until it's collected. A char[] can be overwritten with zeros (Arrays.fill(pw, '\0')) as soon as you've used it.

Q: What's the difference between isEmpty() and isBlank()? A: isEmpty() is true only for a length of 0. isBlank() (Java 11) is also true for strings containing only whitespace.

Q: How do you reverse a string efficiently? A: new StringBuilder(s).reverse().toString(). It runs in linear time, and it handles surrogate pairs correctly.

Q: What does String.join do? A: It concatenates elements with a delimiter: String.join(", ", List.of("a", "b")) → "a, b". For streams, use Collectors.joining(", ").

Previous

Design Patterns & SOLID Basics — Interview Questions

Next

static & final Keywords — Interview Questions

AI Tutor

Lesson: Strings, String Pool, StringBuilder & StringBuffer — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.