The toLowerCase() method in Java is used to convert all the characters in a string to lowercase according to the rules of the default locale. This method does not modify the original string; instead, it returns a new string with all characters converted to lowercase.
Syntax:
public String toLowerCase()
Notes:
- Converts all characters of the string to lowercase.
- It returns a new string where all the characters are converted to lowercase.
- If the string is already in lowercase or if it contains characters that cannot be converted, the original string is returned as is.
Example:
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Convert to lowercase
String lowerStr = str.toLowerCase();
System.out.println("Original String: " + str); // Output: Hello, World!
System.out.println("Lowercase String: " + lowerStr); // Output: hello, world!
}
}
Output:
Original String: Hello, World!
Lowercase String: hello, world!
Lowercase String: hello, world!