Java String

← Core Java | Notes | Home


Java String — Index

Contents

  1. String Literal — SCP, == comparison
  2. String Objectnew keyword, heap memory
  3. String Immutability — thread safety, SCP, security
  4. StringBuilder & StringBuffer — mutable, synchronized, thread-safe

Interview Questions


← Core Java | Notes

1. String Literal

Type of a String Object which is mostly used. Declared directly using String keyword and actual string value in double quotes without using new keyword.

String s1 = "Hello";
String s2 = "Hello";
s1 == s2 // true
  1. String literal object is stored in String Constant Pool insie heap memory.
  2. If String object of same value already exists in the String Constant Pool JVM reuses it and hence it is memory efficient.
  3. == returns true if two String literals have same value (ie same string) because both references to same object in memory.

2. String Object

String object created using new keyword.

String s1 = new String("Hello");
String s2 = new String("Hello");
s1 == s2 // false
  1. It is store in heap memory outside String Constant Pool.
  2. New Object is created everytime

3. String Immutability

  1. Once a String literal or String Object is created, its value cannot be changed.
  2. Any modifications (like concat(), replace(), etc) creates a new String Object instead of modifying the existing one.
    String s1 = "Hello";
    s1.concat(" World");
    System.out.println(s1) // Hello
    

because s1 reference to "Hello". concat() creates a new String Object, but the returned object is not assigned back to s1.

Reasons why String is immutable:

  1. Thread safety
  2. Memory optimization through String Pool reuse
  3. Security (important for class loading, URLs, file paths, etc)
  4. Immutability is a foundation of functional programming

4. StringBuilder and StringBuffer

  1. They are used to modify the same object in memory instead of creating new objects.
  2. They use internal character array that grows dynamically when needed.
    StringBuilder sb = new StringBuilder("Hello");
    sb.append(" World");
    System.out.println(sb) // Hello World
    

append() modifies the same object instead of creating new one.

StringBuilder

  1. Not thread-safe
  2. Faster because there is no synchronization overhead.
  3. Used in single-threaded environment where performance is important.

StringBuffer

  1. Thread-safe
  2. Slighty slower than StringBuilder because methods are synchronized
  3. Used in multithreaded environment

How StringBuffer is thread-safe internally

  1. Most methods in StringBuffer are synchronized.
  2. Only one thread can execute synchronized methods on the same object at a time.
  3. This prevents multiple threads from modifying the internal character array simultaneously.
    public synchronized StringBuffer append(String str)
    

Synchronization ensures thread safety but adds performance overhead.

StringBuilder was introduced in Java 5 as a faster alternative to StringBuffer when thread safety is not required.


Interview Questions

1. What is String Constant Pool?

String Constant Pool (SCP) is a special memory in heap to store String Literals.

So when a new String Literal is is created, jvm checks SCP first :

This reduces unnecessary object creation, saves memory, and improves performance because multiple references can point to the same immutable String Object.

2. What is difference String, StringBuilder and StringBuffer?

3. Why is String Immutable?

Strings are immutable for security, thread-safety and performance reasons.

4. String Literal vs String Object

String Literal :

String Object :

Note : equals() compares content, whereas == compares references.

s1.equals(s2) // true .. even when == is false.

5. When is StringBuffer better than String?

6. What does immutability mean

What Happens Here?

String s1 = "hello";
s1.concat(" world");
System.out.println(s1); // hello

7. How to create a immutable class?


← Core Java | Notes | Home