The toLowerCase() method of the Java String class converts all the uppercase characters of the string into lowercase. It returns a string output and has no required parameters. Furthermore, the toLowerCase() method is called directly via the string variable and is imported from java.util package. Below is the visual representation of this method:
This article illustrates the practical implementation of the toLowerCase() Method in Java.
Why the toLowerCase() Method is Used in Java?
Java falls under the category of case-sensitive programming languages. The term “case-sensitive” refers to the ability of a programming language to differentiate lowercase letters from uppercase letters. This difference in uppercase and lowercase letters results in various exceptions despite providing the correct input. For example, consider the following code to understand this concept:
public class LowerCaseConversion {
public static void main(String[] args) {
String string_1="JavaBeat";
String string_2="javabeat";
if (string_1.equals(string_2))
{
System.out.println("The two strings are equal");
}
else {
System.out.println("The two strings are not equal");
}
}
}
In the code mentioned above:
- The “string_1” and “string_2” are two string variables declared and initialized.
- The if-statement checks whether the provided strings are equal in terms of their content. The else block will execute if the compared strings are not equal.
Output
The two strings will only be equal if the content as well as the letter case is the same. Within the code given below, the “string_1” is converted into lowercase to match the “string_2” using the toLowerCase() method:
public class LowerCaseConversion {
public static void main(String[] args) {
String string_1="JavaBeat";
String string_2="javabeat";
if (string_1.toLowerCase().equals(string_2))
{
System.out.println("The two strings are equal");
}
else {
System.out.println("The two strings are not equal");
}
}
}
Output
How to Use Java String toLowerCase() Method?
There are two different signatures for the toLowerCase() method in Java. The two signatures can be used interchangeably and they only differ in terms of the number of parameters while the basic functionality remains the same.
Syntax
Given below is the basic syntax of the toLowerCase() method of Java:
public String toLowerCase()
Syntax
The following syntax is used to convert a string to lowercase according to the specific locale:
public String toLowerCase(Locale loc)
In the above-mentioned syntax:
- The “Locale” in Java is used to present the information according to a specific geographical area. If no locale is given, the toLowerCaser() method will convert the string using the default locale. The object “loc” in the syntax is created using the Locale class.
Learn more: Java Locale Class Example
Below mentioned are the various examples that discuss the implementation of the toLowerCase() method in Java.
Example 1: Demonstrates the Basic Usage of the toLowerCase() Method in Java
Given below is the code that provides the simple application of the toLowerCase() method in Java:
public class LowerCaseConversion {
public static void main(String[] args) {
String lowerCase="Welcome to JavaBeat";
System.out.println("The lowercase conversion is " + "\""+ lowerCase.toLowerCase()+ "\"");
}
}
In the above-mentioned code:
- Inside the main method of the LowerCaseConversion class, the “lowerCase” is a string variable that is declared and initialized.
- A print statement is used to display the output after converting the string to lowercase via the toLowerCase() method. The “\” is used to print special characters i.e., inverted commas.
Output
Example 2: Demonstrates the Usage of the toLowerCase() Method Via the User Input
Let’s consider an example where we will convert the user input into lowercase letters using the toLowerCase() method. The code is given as follows:
import java.util.*;
public class LowerCaseConversion { //public class that contains the main method
public static void main(String[] args) { //main method for the code execution
Scanner obj=new Scanner(System.in);
System.out.println("Enter a String");
String input=obj.nextLine();
System.out.println("The conversion to lowercase is " + "\"" + input.toLowerCase() + "\"");
}
}
In the above-mentioned code:
- The “import java.util.*” statement is used to include all the classes and functions for taking the input.
- The “obj” is the object of the Scanner class that will be used for taking input from the user via the keyboard.
- The print statement displays the message for the user to enter a string. The string-type variable “input” stores the input of the user using the nextLine() method.
- Finally, the print statement is used to display the string which is converted into the lowercase using the toLowerCase() method.
Output
Example 3: Demonstrates the Usage of the toLowerCase() Method With String, Numbers, and Special Characters
In this example, we are demonstrating the impact of the toLowerCase() method on the combination of a string with numbers and special characters:
public class LowerCaseConversion {
public static void main(String[] args) {
String input="She was here @ 9.45 AM";
System.out.println("The conversion to lowercase is " + "\"" + input.toLowerCase() + "\"");
}
}
In the code mentioned above:
- A string variable “input” is initialized with the given value.
- The “System.out.println()” displays the output and “\”” is used to display the inverted commas that are special characters.
- The input is converted into lowercase by the toLowerCase() method.
Output
Example 4: Demonstrates the Usage of the toLowerCase() Method Via the Locale
Within this example, the toLowerCase() method is given a locale object as a parameter. It will convert the string in lowercase under the influence of the specified locale:
import java.util.*;
public class LowerCaseConversion {
public static void main(String[] args) //main method within the class that will be executed
{
String input="İstanbul";
Locale turkish=Locale.forLanguageTag("TR"); // Locale object with the specific language tag
System.out.println("The conversion to Lowercase without Locale is " + input.toLowerCase());
System.out.println("The conversion to lowercase with Locale is " + "\"" + input.toLowerCase(turkish) + "\"");
}
}
In the code given above:
- The “import java.util.*” statement is used to include the necessary classes and functions such as Locale.
- In the main() method, a string variable “input” is initialized with the value “Istanbul”.
- The forLanguageTag() is the static method of the Locale class. It creates a locale object “turkish”. The language tag “TR” represents the Turkish language.
- The print statements display the comparison of the default and specific locale.
Output
Example 5: Demonstrate the Usage of the toLowerCase() Method With Array
The toLowerCase() method is only used with the string data type. In the example given below, the toLowerCase() method is used with a string array to convert the elements in lowercase at each index:
public class LowerCaseConversion {
public static void main(String[] args) {
String[] Array= {"JAVABEAT","JaVAbEAT","javabeat"};
for (int i=0; i<Array.length;i++)
{
System.out.println("The lowercase conversion is " + Array[i].toLowerCase());
}
}
}
Within the code mentioned above:
- A string array “Array” is declared and initialized in the main() method.
- The for loop iterates over each array index. An integer-type variable “i” is initialized with the value 0. This loop will terminate when the value of “i” exceeds the array’s length. In each iteration, the value of the “i” is incremented.
- The print statements display the output after converting the element at each index in lowercase.
Output
That is all from this guide.
Conclusion
The toLowerCase() method of the String class returns a string in which all the uppercase characters are converted into lowercase letters for a given string. The basic syntax of the toLowerCase() method in Java does not take any argument and applies the default locale while converting. However, the second syntax of this method allows the user to specify a locale which is also syntactically correct. So that the conversion of the characters in lowercase will be according to the given locale.