Launch your tech mastery with us—your coding journey starts now!
Course Content
Data Structure

Because Strings are immutable, simple operations can be costly if done incorrectly. Here is how to handle them.

String operations infographic showing concatenation, substring extraction, length calculation, string reversal, character replacement, and string splitting with visual examples.

Common Operations:

  1. Concatenation: Joining two strings together.
  2. Substring: Extracting a portion of a string.
  3. Length: Counting the number of characters.
  4. Searching: Finding the index of a specific character or pattern.

Java Implementation: String Methods

public class StringBasics {
    public static void main(String[] args) {
        // 1. Creation
        String str1 = "Hello";              // String Literal (Stored in Pool)
        String str2 = new String("World");  // String Object (Stored in Heap)

        // 2. Concatenation
        String result = str1 + " " + str2; 
        System.out.println(result); // Output: Hello World

        // 3. Length (O(1))
        System.out.println("Length: " + result.length()); // Output: 11

        // 4. CharAt (Accessing character at index - O(1))
        System.out.println("Char at index 1: " + str1.charAt(1)); // Output: e

        // 5. Substring (Extracting part - O(n))
        // substring(start, end) -> includes start, excludes end
        System.out.println("Substring: " + result.substring(0, 5)); // Output: Hello

        // 6. Equality Check (Critical in Java!)
        // ALWAYS use .equals(), NEVER use == for strings
        System.out.println(str1.equals("Hello")); // true
    }
}