SyntaxStudy
Sign Up
Java Strings, StringBuilder, and Variable Scope
Java Beginner 1 min read

Strings, StringBuilder, and Variable Scope

Strings in Java are immutable objects of the java.lang.String class. Once a String is created its character sequence cannot be changed. Operations like concatenation and replace return new String objects. Because of immutability, String objects are safe to share between threads, and the JVM maintains a string pool so that string literals with the same content often share the same object. When you need to build a string incrementally, use StringBuilder instead of concatenating Strings in a loop. Each + concatenation creates a new String object; a loop with 1000 concatenations generates 1000 intermediate objects. StringBuilder maintains an internal character buffer and exposes methods like append, insert, delete, and reverse. Call toString() at the end to get the finished String. Variable scope determines where a variable is accessible. Local variables are declared inside a method or block and are only visible within that block. Instance variables belong to an object and are visible to all methods of the class. Class (static) variables belong to the class itself and exist independently of any instance. Java also supports the final keyword to declare constants; by convention, constants are written in UPPER_SNAKE_CASE.
Example
public class StringsAndScope {

    // Class-level constant (static final)
    private static final int MAX_ITEMS = 100;

    // Instance variable
    private String label;

    public StringsAndScope(String label) {
        this.label = label;  // 'this' disambiguates instance vs parameter
    }

    public void demonstrateStrings() {
        // String immutability
        String s = "hello";
        s.toUpperCase();         // returns new String, does NOT modify s
        System.out.println(s);   // still "hello"

        String upper = s.toUpperCase(); // must capture return value
        System.out.println(upper);      // "HELLO"

        // Common String methods
        String text = "  Java Programming  ";
        System.out.println(text.trim());           // "Java Programming"
        System.out.println(text.trim().length());  // 16
        System.out.println(text.contains("Java")); // true
        System.out.println(String.join(", ", "a", "b", "c")); // "a, b, c"

        // StringBuilder for efficient concatenation
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            sb.append("item").append(i);
            if (i < 5) sb.append(", ");
        }
        System.out.println(sb.toString()); // "item1, item2, item3, item4, item5"
        sb.reverse();
        System.out.println(sb.toString());

        // Block scope demo
        int outer = 10;
        {
            int inner = 20; // only visible inside this block
            System.out.println("inner + outer = " + (inner + outer));
        }
        // System.out.println(inner); // compile error — inner is out of scope
        System.out.println("MAX_ITEMS = " + MAX_ITEMS);
        System.out.println("label = " + label);
    }

    public static void main(String[] args) {
        new StringsAndScope("demo").demonstrateStrings();
    }
}