String toUpperCase:

You can convert the whole string into the upper case by using the toUpperCase() method in Java. Here we will discuss some simplest examples of the toUpperCase() method in Java.

Method signature:

The signature for a toUpperCase() method is as shown below.

public String toUpperCase()
public String toUpperCase(Locale locale)

Method parameters and return type:

Return type: It returns a string in upper case letters.

Parameters: For the first signature, it doesn’t take any parameters. For the second signature, we pass the locale object as a parameter.

Throws: It throws a NullPointerException if the string is null.

Example 1:

Here we have simply converted a string into a lower case.

Source code:

public class Example1 {

    public static void main(String args[]) {
        String str = "learN Java code";
        String toUpper = str.toUpperCase();
        System.out.println(toUpper);
    }
}

Output:

LEARN JAVA CODE
BUILD SUCCESSFUL (total time: 9 seconds)

Example 2:

We can also pass an argument in this method of a locale type for different languages.

Source code:

public class Example2 {
    
    public static void main(String[] args) {  
        String str = "welcome to Java TUtorial";    
        String engStr = str.toUpperCase(Locale.ENGLISH);  
        System.out.println(engStr);   
    }  
}

Output:

run:
WELCOME TO JAVA TUTORIAL
BUILD SUCCESSFUL (total time: 1 second)

References:

Leave A Comment