Java

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”:

public class JavaCaseChanger{
……
}

Now, in the main function we are displaying a message on the screen using System.out.println() function:

System.out.println("Java Lower Case Example 1");

Now, the next step is to initialize a string value that you want to convert to lowercase:

String str="THIS TEXT IS IN CAPITAL LETTERS";

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:

String strLower=str.toLowerCase();

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.

$ nano JavaCaseChanger.java

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:

$ javac JavaCaseChanger.java
$ java JavaCaseChanger

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