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

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.