Java String toLowerCase() Method Example

1. Introduction

The String.toLowerCase() method in Java is used to convert all the characters in a string to lowercase. This method is often used in data normalization processes, especially in cases where case-insensitive comparisons are necessary. This tutorial will demonstrate how to use toLowerCase() to convert a string to lowercase.

Key Points

- toLowerCase() converts all characters in the string to lowercase.

- It is useful for text processing and ensuring consistency in user input or data storage.

- The method returns a new string, leaving the original string unchanged.

2. Program Steps

1. Declare a string with mixed case.

2. Convert the string to lowercase.

3. Print the original and the converted string.

3. Code Program

public class StringToLowerCaseExample {
    public static void main(String[] args) {
        // Original string with mixed case
        String original = "Hello, WORLD!";
        // Convert to lowercase
        String lowercased = original.toLowerCase();
        // Print original and lowercase version
        System.out.println("Original: " + original);
        System.out.println("Lowercase: " + lowercased);
    }
}

Output:

Original: Hello, WORLD!
Lowercase: hello, world!

Explanation:

1. String original = "Hello, WORLD!": Declares and initializes a String variable original with both uppercase and lowercase letters.

2. String lowercased = original.toLowerCase(): Converts all characters in original to lowercase and assigns the result to lowercased.

3. System.out.println("Original: " + original): Prints the original string.

4. System.out.println("Lowercase: " + lowercased): Prints the new string after conversion to lowercase, demonstrating the effect of the toLowerCase() method.


Comments