Java String toLowerCase() method example

The Java String toLowerCase() returns a string which is result of converting all characters in given string to lowercase.

1. String toLowerCase() method

Use String.toLowerCase() converts any string to lowercase letters.

1.1. Method syntax

String toLowerCase() method is overloaded and comes in two variants.

/**
* @param locale - locale use the case transformation rules for given locale
* 
* @return - string converted to lowercase
*/
public String toLowerCase();

public String toLowerCase(Locale locale);

1.2. ‘null’ is not valid method argument

Method does not accept 'null' argument. It will throw NullPointerException in case method argument is null.

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.toLowerCase(String.java:2710)
	at com.StringExample.main(StringExample.java:11)

2. Java convert string to lowercase example

Java program to convert the string to lowercase using default locale rules.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String string = "Hello World";
        
        String lowercaseString = string.toLowerCase();
        
        System.out.println(lowercaseString);
    }
}

Program output.

hello world

toLowerCase() method is equal to calling toLowerCase(Locale.getDefault()).

3. Java String toLowerCase(Locale locale) example

Java program to convert the string to lowercase using default locale rules.

public class StringExample 
{
    public static void main(String[] args) 
    {
        System.out.println("hello world".toLowerCase(Locale.getDefault()));
        System.out.println("Γειά σου Κόσμε".toLowerCase(Locale.US));
    }
}

Program output.

hello world
γειά σου κόσμε

In this example, we learned to convert string to lowercase.

References:

A Guide to Java String
String Java Doc

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial