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!
Java String toLowerCase() method – Interview Questions
Q 1: What does toLowerCase() method do in Java?
Ans: It converts all characters of a string into lowercase.
Q 2: Does toLowerCase() modify the original string?
Ans: No, it returns a new string because strings are immutable.
Q 3: Is toLowerCase() case-sensitive?
Ans: No, it performs case conversion.
Q 4: Does toLowerCase() affect numbers or symbols?
Ans: No, only alphabetic characters are converted.
Q 5: Is toLowerCase() locale-dependent?
Ans: Yes, it can be affected by locale settings.
Java String toLowerCase() method – Objective Questions (MCQs)
Q1. What does the toLowerCase() method do?
Q2. What will "HELLO".toLowerCase() return?
Q3. Which of the following is true about toLowerCase()?
Q4. What will happen if toLowerCase() is called on an empty string?
Q5. Which of the following correctly converts a string variable str to lowercase?