Java String toUpperCase() method example

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

Read more: Java example capitalize the string

1. String toUpperCase() method

Use String.toUpperCase() converts any string to UPPECASE letters.

1.1. Method syntax

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

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

public String toUpperCase(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.toUpperCase(String.java:2710)
	at com.StringExample.main(StringExample.java:11)

2. Java convert string to uppercase example

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

public class StringExample 
{
    public static void main(String[] args) 
    {
        String string = "hello world";
        
        String uppercaseString = string.toUpperCase();
        
        System.out.println(uppercaseString);
    }
}

Program output.

HELLO WORLD

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

3. Java String toUpperCase(Locale locale) example

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

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

Program output.

HELLO WORLD
ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ

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

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