String Creation and Manipulation

In Java, String is a widely used class that represents a sequence of characters. The String class is immutable, meaning once a String object is created, its value cannot be modified.

Java provides several ways to create and manipulate strings using constructors, operators, and built-in methods.

1. String Creation

Using String Literals:

  • When a string is created using double quotes (""), it is stored in the String Pool.
String str1 = "LotusJavaPrince";
String str2 = "LotusJavaPrince";  // Refers to the same object as str1Code language: JavaScript (javascript)

Using the new Keyword:

  • This creates a new object in the heap, even if the same value exists in the String Pool.

String str3 = new String("LotusJavaPrince");Code language: JavaScript (javascript)

Using char[] Array:

char[] chars = {'M', 'a', 'h', 'e', 's', 'h'};
String str4 = new String(chars);Code language: JavaScript (javascript)

Using StringBuilder or StringBuffer:

StringBuilder sb = new StringBuilder("Java");
String str5 = sb.toString();Code language: JavaScript (javascript)

2. String Comparison

  • Using == Operator: Checks for reference equality.
  • Using .equals() Method: Checks for content equality.
  • Using .compareTo() Method: Compares lexicographically.
String str1 = "Mahesh";
String str2 = "Mahesh";
String str3 = new String("Mahesh");

// Reference Comparison
System.out.println(str1 == str2);     // true
System.out.println(str1 == str3);     // false

// Content Comparison
System.out.println(str1.equals(str3));  // true

// Lexicographical Comparison
System.out.println(str1.compareTo("Java"));  // Positive value (str1 > "Java")Code language: JavaScript (javascript)

3. String Manipulation Methods

Example Program

public class StringManipulationDemo {
    public static void main(String[] args) {
        String str1 = "   LotusJavaPrince   ";
        String str2 = "Mahesh";

        // Trim and Length
        String trimmed = str1.trim();
        System.out.println("Trimmed: '" + trimmed + "'");
        System.out.println("Length: " + trimmed.length());

        // Concatenation
        String combined = trimmed.concat(" - ").concat(str2);
        System.out.println("Concatenated: " + combined);

        // Substring
        String subStr = combined.substring(6, 16);
        System.out.println("Substring (6, 16): " + subStr);

        // Character Access
        char ch = str2.charAt(3);
        System.out.println("Character at index 3 in '" + str2 + "': " + ch);

        // Replace
        String replaced = combined.replace('a', '@');
        System.out.println("Replaced 'a' with '@': " + replaced);

        // Uppercase and Lowercase
        System.out.println("Uppercase: " + combined.toUpperCase());
        System.out.println("Lowercase: " + combined.toLowerCase());

        // Checking Content
        boolean containsJava = combined.contains("Java");
        System.out.println("Contains 'Java': " + containsJava);

        // Index of
        int index = combined.indexOf("Prince");
        System.out.println("Index of 'Prince': " + index);

        // Last Index of
        int lastIndex = combined.lastIndexOf('e');
        System.out.println("Last index of 'e': " + lastIndex);
    }
}
/*
Trimmed: 'LotusJavaPrince'
Length: 14
Concatenated: LotusJavaPrince - Mahesh
Substring (6, 16): aPrince - M
Character at index 3 in 'Mahesh': e
Replaced 'a' with '@': LotusJ@v@Prince - M@hesh
Uppercase: LOTUSJAVAPRINCE - MAHESH
Lowercase: lotusjavaprince - mahesh
Contains 'Java': true
Index of 'Prince': 8
Last index of 'e': 24
*/

5. String Immutability:

In Java, strings are immutable, meaning once a string object is created, it cannot be modified.

String original = "Java";
original.concat("Planet");
System.out.println(original);  // Output: JavaCode language: JavaScript (javascript)

The concat() method does not modify the original string but returns a new string object.

Java provides a comprehensive set of string manipulation methods for creating, modifying, and accessing strings. Understanding string immutability, comparison, and the use of StringBuilder or StringBuffer is crucial for optimal memory management and performance.

For more practice…

String class

Java provides a comprehensive set of string manipulation methods for creating, modifying, and accessing strings. Understanding string immutability, comparison, and the use of StringBuilder or StringBuffer is crucial for optimal memory management and performance.

Scroll to Top