Java Strings

Explore the String class, immutability, and common string manipulation methods.

Strings represent text in Java and are one of the most commonly used data types. They are immutable, meaning once created, they cannot be changed.

1. Creating Strings

You can create strings using string literals or using the new keyword.

Try example:

public class StringCreateDemo {
    public static void main(String[] args) {

        String s1 = "Hello VINAR TECH";
        String s2 = new String("Welcome to Java");

        System.out.println(s1);
        System.out.println(s2);
    }
}

2. Important String Methods

The String class provides many helpful operations for text handling:

  • length() - number of characters
  • charAt(int index) - character at given index
  • substring(start, end) - extract part of string
  • equals(String) - compare actual value
  • compareTo(String) - alphabetical comparison
  • toUpperCase() / toLowerCase()
  • trim() - removes leading/trailing spaces
  • split(String) - convert string into array

Try example:

public class StringMethodsDemo {
    public static void main(String[] args) {

        String txt = "  Hello World from VINAR TECH  ";

        System.out.println("Length: " + txt.length());
        System.out.println("Trimmed: '" + txt.trim() + "'");
        System.out.println("Uppercase: " + txt.toUpperCase());
        System.out.println("Substring (6 to 11): " + txt.substring(6, 11));
        System.out.println("Index of 'World': " + txt.indexOf("World"));

        String[] words = txt.trim().split(" ");
        System.out.println("Words:");
        for (String w : words) {
            System.out.println(w);
        }
    }
}

3. String Comparison

Use equals() for value comparison (recommended). Use == to compare references (memory location).

Try example:

public class StringCompareDemo {
    public static void main(String[] args) {

        String a = "VINAR";
        String b = "VINAR";
        String c = new String("VINAR");

        System.out.println("a == b : " + (a == b));      // True (both in string pool)
        System.out.println("a == c : " + (a == c));      // False (different objects)
        System.out.println("a.equals(c) : " + a.equals(c)); // True (same value)
    }
}

4. String Immutability

Each modification creates a new object in memory.

Try example:

public class StringImmutableDemo {
    public static void main(String[] args) {

        String s = "Hello";
        System.out.println("Original: " + s);

        s.concat(" VINAR TECH");  // Not stored
        System.out.println("After concat (unchanged): " + s);

        s = s.concat(" VINAR TECH");  
        System.out.println("After storing concat: " + s);
    }
}

5. StringBuilder & StringBuffer

Use when you need mutable strings (frequent modifications).

A. StringBuilder (Faster, Not Thread-Safe)

Try example:

public class StringBuilderDemo {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("VINAR");
        sb.append(" TECH Solutions");

        System.out.println(sb);
    }
}
B. StringBuffer (Thread-Safe)

Try example:

public class StringBufferDemo {
    public static void main(String[] args) {

        StringBuffer sbf = new StringBuffer("Secure");
        sbf.append(" Operations");

        System.out.println(sbf);
    }
}
Sidebar Ad Space
Advertisement Space