Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Java Strings

String Immutability

In Java, String objects are immutable — once created, their value cannot be changed. Every operation that appears to modify a string actually creates a new String object. Strings are stored in the String Pool (a special area of the heap) for memory efficiency.

Common String Methods
public class StringMethods {
    public static void main(String[] args) {
        String s = "  Hello, Java World!  ";

        System.out.println(s.length());              // 22
        System.out.println(s.trim());                // "Hello, Java World!"
        System.out.println(s.trim().toLowerCase());  // "hello, java world!"
        System.out.println(s.trim().toUpperCase());  // "HELLO, JAVA WORLD!"

        String t = s.trim();
        System.out.println(t.charAt(7));             // J
        System.out.println(t.indexOf("Java"));       // 7
        System.out.println(t.contains("World"));     // true
        System.out.println(t.substring(7, 11));      // Java
        System.out.println(t.replace("Java", "Core Java")); // Hello, Core Java World!
        System.out.println(t.startsWith("Hello"));   // true
        System.out.println(t.endsWith("!"));         // true

        // split
        String csv = "one,two,three,four";
        String[] parts = csv.split(",");
        for (String p : parts) System.out.print(p + " ");  // one two three four
        System.out.println();

        // equals vs ==
        String a = new String("test");
        String b = new String("test");
        System.out.println(a == b);       // false (different objects)
        System.out.println(a.equals(b));  // true  (same content)
    }
}

StringBuilder vs StringBuffer

When you need to build or modify strings frequently, use StringBuilder (single-threaded) or StringBuffer (thread-safe). Both are mutable, unlike String.

FeatureStringStringBuilderStringBuffer
MutableNoYesYes
Thread-safeYes (immutable)NoYes (synchronized)
PerformanceSlow for concatenationFastSlower than StringBuilder
Use whenFixed textSingle-threaded string buildingMulti-threaded string building
StringBuilder & String.format()
public class StringBuilderDemo {
    public static void main(String[] args) {
        // StringBuilder — efficient mutable string
        StringBuilder sb = new StringBuilder();
        sb.append("Hello");
        sb.append(", ");
        sb.append("World");
        sb.append("!");
        System.out.println(sb.toString());       // Hello, World!
        System.out.println(sb.length());         // 13
        sb.insert(5, " Java");
        System.out.println(sb.toString());       // Hello Java, World!
        sb.delete(5, 10);
        System.out.println(sb.toString());       // Hello, World!
        sb.reverse();
        System.out.println(sb.toString());       // !dlroW ,olleH

        // String.format() — printf-style formatting
        String name = "Alice";
        int age = 30;
        double gpa = 3.85;
        String formatted = String.format("Name: %-10s | Age: %3d | GPA: %.2f", name, age, gpa);
        System.out.println(formatted);
        // Name: Alice      | Age:  30 | GPA: 3.85
    }
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.