- Java — Character toLowerCase() Method
- Syntax
- Parameters
- Return Value
- Example
- Output
- Example
- Output
- Example
- Output
- Example
- Output
- Java String toLowerCase() Method
- Definition and Usage
- Syntax
- Parameters
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- toLowerCase() in Java
- Syntax of toLowerCase() in Java
- Parameters of toLowerCase() in Java
- Return Value of toLowerCase() in Java
- Exceptions of toLowerCase() in Java
- Examples
- Example — 1 : Returns String in Lowercase Letter
- Example — 2 : Returns String in Lowercase Letter
- Internal Working of toLowerCase() in Java
- toLowerCase() with Locale Parameter
- Syntax :
- Parameters :
- Returns :
- Example :
- Conclusion
- How to Use toLowerCase Function in Java
- Using toLowerCase function in Java
- Using toUpperCase function in Java
- Conclusion
- About the author
- Taimoor Mohsin
Java — Character toLowerCase() Method
The Java Character toLowerCase() method converts the character (Unicode code point) argument to lowercase using case mapping information from the UnicodeData file.
According to UnicodeData file, case is defined as the inherent property of a character. Case mappings in this file are informative and default mappings. For example, if a character is CAPITAL by default, then its corresponding lowercase is informative.
This method does not convert all characters in Unicode to their lowercase; especially characters like symbols or ideographs.
Note − There are two polymorphic forms of this method, with different parameter types.
Syntax
Following is the syntax for Java Character toLowerCase() method
public static int toLowerCase(int codePoint) (or) public static char toLowerCase(char ch)
Parameters
- codePoint − the Unicode code point to be converted
- ch − the character to be converted
Return Value
This method returns the lowercase equivalent of the character or Unicode code point, if any; otherwise, the character itself.
Example
The following example shows the usage of Java Character toLowerCase(int codePoint) method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo < public static void main(String[] args) < // create 4 int primitives int cp1, cp2, cp3, cp4; // assign values to cp1, cp2 cp1 = 0x0057; cp2 = 0x2153; // assign lowercase of cp1, cp2 to cp3, cp4 cp3 = Character.toLowerCase(cp1); cp4 = Character.toLowerCase(cp2); String str1 = "Lowercase equivalent of cp1 is " + cp3; String str2 = "Lowercase equivalent of cp2 is " + cp4; // print cp3, cp4 values System.out.println( str1 ); System.out.println( str2 ); >>
Output
Let us compile and run the above program, this will produce the following result −
Lowercase equivalent of cp1 is 119 Lowercase equivalent of cp2 is 8531
Example
The following example shows the usage of Java Character toLowerCase(char ch) method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo < public static void main(String[] args) < // create 4 char primitives char ch1, ch2, ch3, ch4; // assign values to ch1, ch2 ch1 = 'P'; ch2 = 's'; // assign lowercase of ch1, ch2 to ch3, ch4 ch3 = Character.toLowerCase(ch1); ch4 = Character.toLowerCase(ch2); String str1 = "Lowercase of " + ch1 + " is " + ch3; String str2 = "Lowercase of " + ch2 + " is " + ch4; // print ch3, ch4 values System.out.println( str1 ); System.out.println( str2 ); >>
Output
Let us compile and run the above program, this will produce the following result −
Lowercase of P is p Lowercase of s is s
Example
In the following example, we write a program that uses conditional statements to check whether the arguments of the method can be converted to lowercase letters or not.
import java.lang.*; public class CharacterDemo < public static void main(String[] args) < char ch, ch2; ch = '4'; if(Character.getType(ch) == 1 || Character.getType(ch) == 2)< ch2 = Character.toLowerCase(ch); System.out.println("Lowercase of " + ch + " is " + ch2); >else System.out.println("The character cannot be converted to Lowercase"); > >
Output
Compile and run the given program above to obtain the output as follows −
The character cannot be converted to Lowercase
Example
As we have discussed in ths article earlier, the method does not return true for some characters like symbols and ideographs. We will understand the scenario in the following example program.
import java.lang.*; public class Main < public static void main(String[] args) < char ch, result; ch = '%'; result = Character.toLowerCase(ch); System.out.println("Lowercase of " + ch + " is " + result); >>
Output
The output for the above program will be printed as follows −
Java String toLowerCase() Method
Convert a string to upper case and lower case letters:
String txt = "Hello World"; System.out.println(txt.toUpperCase()); System.out.println(txt.toLowerCase());
Definition and Usage
The toLowerCase() method converts a string to lower case letters.
Note: The toUpperCase() method converts a string to upper case letters.
Syntax
public String toLowerCase()
Parameters
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
toLowerCase() in Java
The toLowerCase() method in java is a String class method. It is used to convert all letters of a string into lowercase letters. This method is a String class method that is present in the java.util package. Here is the hierarchy of the toLowerCase() method in java.
There are two types of toLowerCase() methods in java :
- toLowerCase() :
Converts all characters into lowercase alphabets. - toLowerCase(Locale loc) :
Converts all the characters into lowercase according to the given Locale. The locale specifies the language we want the output in or any other rules.
Syntax of toLowerCase() in Java
Parameters of toLowerCase() in Java
The toLowerCase() method in java does not take any parameters. Here we don’t pass any parameter to the toLowerCase() method in java. We call the method using the string object.
Return Value of toLowerCase() in Java
Return Type : String
The toLowerCase() method in java returns a String converted into lowercase. While returning the string to lowercase, all uppercase letters are swapped with lowercase and the previous lowercase characters are kept untouched.
Exceptions of toLowerCase() in Java
The toLowerCase() method in java does not throw any exceptions.
Examples
Let’s understand how to convert a string into lowercase using the toLowerCase() method in java.
Example — 1 : Returns String in Lowercase Letter
In the above example, we have declared a string s1 with a sentence with both uppercase and lowercase letters. This string s1 is converted into lowercase letters using s1.toLowerCase() and stored in String s2 . Finally, we have printed s2 which holds the converted lowercase string.
Example — 2 : Returns String in Lowercase Letter
In this example, we have created a string object s1 that holds the string to be converted. And converted the string into lowercase letters using s1.toLowerCase(); directly in the print statement.
Internal Working of toLowerCase() in Java
The class String includes methods for examining individual characters of the sequence, comparing strings, searching strings, extracting substrings, and creating a copy of a string with all characters translated to uppercase or lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.
The internal implementation of the toLowerCase() method of the String class in Java is as follows :
The above code takes a Locale object and returns a String in lowercase form. It first checks if the Locale object is null and throws a NullPointerException if it is. The method then iterates over each character in the String and checks if it needs to be changed to lowercase according to the rules of the specified Locale object.
If there are any characters that need to be changed, the method creates a new character array with the lowercase characters and returns a new String. If no characters need to be changed, the method simply returns the original String. The method takes into account special cases where certain characters need to be mapped to multiple lowercase characters, such as the Turkish dotless i or the Greek capital letter sigma. It also handles characters outside the Basic Multilingual Plane by growing the result array if needed.
toLowerCase() with Locale Parameter
The toLowerCase() with Locale Parameter converts all the characters of the string into lowercase characters using the given Locale such as languages, country code, etc.
Locale : A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.
Syntax :
Parameters :
The toLowerCase() with Locale method in java takes Locale as a parameter. This Locale sets rules for the output string.
Returns :
The toLowerCase() with Locale method in java returns String converted into lowercase as per the given Locale.
Example :
Now let’s see an example to convert string letters into lowercase using the toLowerCase() method with Locale. Here, we will try to print output in a different language like ‘Turkish’ using the Locale parameter.
This article covers toLowerCase() method in Java. Two types of the toLowerCase() method are covered in this article, without parameters, and with locale parameters.
In this example, we have used the toLowerCase method in Java along with Locales. Here we have created locales for two languages Turkish and English using Locale.forLanguageTag(«tr»); and converted it into lower case using s1.toLowerCase(tur); . The final output is displayed in the output section.
Conclusion
- The toLowerCase() is a method of String class located in the java.util package in Java.
- It converts all letters of String into lowercase and returns it in the form of String.
- The toLowerCase() method neither takes any argument nor throws any exception.
- We can convert a string into the required language using the toLowerCase(Locale L) method. It takes Locale as a parameter and returns a string with lowercase letters as per rules defined by the locale.
How to Use toLowerCase Function in Java
Lower case strings indicate that you have written something in upper case letters and want to alter it to small case letters, whereas upper case strings indicate that you have written something in small letters and want to change it to capital letters. The main advantage of writing in lowercase letters is that it provides less strain on the eyes and makes text simpler to read. However, under today’s writing standards, we have to use upper case letters as well, particularly when beginning a phrase or if you want to put emphasis on any specific word.
Java provides two important case-changing methods; the toLowerCase() converts the string to lower case while the toUpperCase() changes the string to uppercase (capital) letters that will be discussed in this article.
Using toLowerCase function in Java
To start the code we first create the class with the name “JavaCaseChanger”:
Now, in the main function we are displaying a message on the screen using System.out.println() function:
Now, the next step is to initialize a string value that you want to convert to lowercase:
As you can see, we have written a line in a capital letter with a data type of ‘String’ and stored this value inside a variable with the name of ‘str’.
Now there is a built-in function in Java called “toLowerCase()” which can be used to make any string into small letters:
Now in the above line, we have written an ‘str.toLowerCase()’ function in which variable ‘str’ represents the string value which we want to convert to lower-case, and then we use ‘.toLowerCase’ built-in function. Now the complete code can be seen below:
public class JavaCaseChanger {
public static void main ( String [ ] args ) {
System . out . println ( «Java Lower Case Example 1» ) ;
String str = «THIS TEXT IS IN CAPITAL LETTERS» ;
System . out . println ( «String in Upper Case: » + str ) ;
String strLower = str. toLowerCase ( ) ;
System . err . println ( «String in Lower Case: » + strLower ) ;
System . out . println ( «» ) ;
}
Now to implement this code you need to create a java file first and then write a code inside it and then save it as well using any text editor as in our case we are using a nano text editor.
Note: Before working with any java file, you need to make sure that java development kit (JDK) has been installed in your operating system.
After that, you need to compile the file first to execute it by typing:
Using toUpperCase function in Java
Similarly, you can also change the string value to the upper or capital case as well and for that, you need to use a toUppercase() function as shown below:
public class JavaCaseChanger {
public static void main ( String [ ] args ) {
System . out . println ( «Java Upper Case Example 1» ) ;
String str = «this text is in small letters» ;
System . out . println ( «String in Lower Case: » + str ) ;
String strUpper = str. toUpperCase ( ) ;
System . out . println ( «String in Upper Case: » + strUpper ) ;
System . out . println ( «» ) ;
}
} //class
Similar to the previous example, we will create a java file first in the nano editor and then compile and execute it as shown below.
Conclusion
Text formatting is crucial for users to access information or data more comprehensively. The primary benefit of writing in lowercase letters is that it makes content easier to read. In this article, we have explained how you can change the string values to lower case or to upper case in Java. To change a value to the lower case there is a built-in function that can be used with a name of toLowerCase() whereas toUpperCase() can be used to change the string to the upper case.
About the author
Taimoor Mohsin
Hi there! I’m an avid writer who loves to help others in finding solutions by writing high-quality content about technology and gaming. In my spare time, I enjoy reading books and watching movies.