🔷 1. String

Description:

  • Immutable class in java.lang.
  • Once created, its value cannot be changed.
  • Most commonly used to represent text.

🔧 Common Methods:

Method Description
length() Returns the number of characters
charAt(int index) Returns character at index
substring(int start, int end) Returns substring
equals() / equalsIgnoreCase() Compares strings
toUpperCase() / toLowerCase() Case conversion
trim() Removes leading/trailing whitespace
replace() Replaces characters or substring

📝 Example:

					String s = "Hello World";
System.out.println(s.length()); // 11
System.out.println(s.substring(0, 5)); // Hello
System.out.println(s.toUpperCase()); // HELLO WORLD

					

🔷 2. String Literal

Description:

  • A string Declared using quotes ("text").
  • Stored in the String Constant Pool.
  • Saves memory by reusing the same object for identical literals.

📝 Example:

String a = "Java";
String b = "Java";
System.out.println(a == b); // true (points to same object in memory)

					
Even though a and b look like different variables, the JVM reuses the string object from the pool.

🔷 3. StringBuilder (Java 1.5+)

Description:

  • Mutable character sequence.
  • Used for efficient string manipulation in a single-threaded environment.
  • Faster than String and StringBuffer.
Method Description
append(String) Adds text at the end
insert(int, String) Inserts text at position
replace(int, int, String) Replaces part of string
delete(int, int) Deletes a range
reverse() Reverses the content
toString() Converts to String

📝 Example:

					
StringBuilder sb = new StringBuilder("Java");
sb.append(" Programming");
System.out.println(sb); // Java Programming
sb.replace(5, 16, "Coding");
System.out.println(sb); // Java Coding
sb.reverse();
System.out.println(sb); // gnidoC avaJ

					

🔷 4. StringBuffer

Description:

  • Mutable and thread-safe version of StringBuilder.
  • All methods are synchronized.
  • Slower than StringBuilder in single-threaded use.
Method Description
append() Adds text
insert() Inserts at position
delete() Remove characters
replace() Replace part of string
reverse() Reverse string
toString() Converts to String

📝 Example:

StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Hello World

					

📌 5. StringJoiner (Java 8+)

Description:

  • Used to join strings with delimiters
  • Can also specify prefix and suffix.
  • Very helpful for generating CSVs, lists, etc.

📝 Constructors:

new StringJoiner(delimiter)
new StringJoiner(delimiter, prefix, suffix)

					

🔧 Common Methods:

Method Description
add(String) Adds a new element
toString() Returns the final joined string
merge(StringJoiner ) Merges another StringJoiner

📝 Example:

StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("Java").add("Python").add("C++");
System.out.println(joiner); // [Java, Python, C++]

					

✅ Comparison of String, StringBuilder, StringBuffer, String Literal, and StringJoiner

Feature String String Literal StringBuilder StringBuffer StringJoiner
Mutability Immutable Immutable Mutable Mutable Mutable
Thread-safe N/A N/A ❌ No ✅ Yes ❌ No
Performance Slower in loops Same as String Fast (single-threaded) Slower (due to sync) Fast (used for joining)
Introduced in Java 1.0 Java 1.0 Java 1.5 Java 1.0 Java 8
Use Case General text Compile-time reuse Efficient concat in loops Thread-safe concat Joining strings with delimiters