☕ Java

Strings in Java

Strings are everywhere — usernames, emails, messages, API responses, file paths. Java treats String as a special class, not just a primitive, and understanding how it works (especially immutability) will save you from subtle bugs.

String Basics and Common Methods

String in Java is an object, not a primitive. It's immutable — once created, the characters inside can't be changed. Any operation that "modifies" a String actually creates a new one. This might seem weird, but it has real benefits: Strings are thread-safe, they can be safely shared, and Java can cache them in a "String Pool" for memory efficiency.
Java
String s = "Hello, Java!";

System.out.println(s.length());           // 12 — number of characters
System.out.println(s.toUpperCase());      // HELLO, JAVA!
System.out.println(s.toLowerCase());      // hello, java!
System.out.println(s.charAt(0));          // H — character at index 0
System.out.println(s.substring(7, 11));   // Java — chars from index 7 to 10
System.out.println(s.contains("Java"));   // true
System.out.println(s.replace("Java", "World")); // Hello, World!
System.out.println(s.trim());             // removes leading/trailing whitespace
System.out.println(s.startsWith("Hello")); // true
System.out.println(s.indexOf("Java"));    // 7 — position where "Java" starts

The Golden Rule: Never Use == to Compare Strings

This is one of the most common Java bugs. == checks if two variables point to the same object in memory. .equals() checks if the content is the same. You almost always want .equals().
Java
String a = new String("hello");
String b = new String("hello");

System.out.println(a == b);             // false! — different objects in memory
System.out.println(a.equals(b));        // true  — same content ✓
System.out.println(a.equalsIgnoreCase("HELLO")); // truecase-insensitive compare

// String literals from the pool CAN match with ==, but don't rely on it
// Always use .equals() for content comparison — make it a habit

StringBuilder — When You Need Mutable Strings

Remember, String is immutable. If you're building a string in a loop (like joining 1000 names), using + operator creates a new String object on every iteration — which hammers performance and memory. StringBuilder is mutable. It lets you modify the same object in place, making it dramatically faster for string building operations.
Java
// Bad — creates a new String on every iteration (slow for large loops)
String result = "";
for (int i = 0; i < 1000; i++) {
    result += i + ",";  // 1000 new String objects created!
}

// Good — StringBuilder modifies in place
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
    sb.append(i).append(",");
}
String result2 = sb.toString();  // Convert to String only at the end

// Other StringBuilder methods
StringBuilder name = new StringBuilder("Hello");
name.append(" World");       // Hello World
name.insert(5, ",");         // Hello, World
name.replace(0, 5, "Hi");    // Hi, World
name.delete(2, 4);           // Hi World
name.reverse();              // dlroW iH