Java toUpperCase

The Java toUpperCase Method is one of the String Methods that helps convert the given string into Uppercase letters. This article will show how to use String toUpperCase with an example. The syntax of it is as shown below.

Java provides two different methods to convert the specified string into Uppercase letters. The following method will not accept any parameters and convert all the characters in a string to uppercase using the rule of the default locale.

public String toUpperCase(); // It will return String 

//In order to use in program
String_Object.toUpperCase()

The following Java toUpperCase Method function will accept the Locale value as the parameter and convert all the characters to uppercase using the user-specified locale variable.

public String toUpperCase(Locale locale);

//In order to use in program
String_Object.toUpperCase(Locale locale)
  • String_Object: Please specify the valid String Object.
  • locale: Please set the Locale you want to use while converting the String_Object characters to Uppercase. For example, English, Turkish, Latin, etc.

Java toUpperCase Method Example

The string toUpperCase method converts the characters to uppercase. This program will help to understand the same.

The first four statements will convert the previously declared String into uppercase and assign it to Str1. Next, we used the toUpperCase String Method directly on string data. And the following Java System.out.println statements will print the output. Check Java toLowerCase and Java equals functions.

package StringFunctions;

public class ToUpperCaseMethod {
 public static void main(String[] args) {
 String str = "Java ProgramminG Tutorial at Tutorial GateWay";
 
 String str1 = str.toUpperCase();
 String str2 = "Java Programming Language".toUpperCase();
 String str3 = "PRogramming WOrld".toUpperCase();
 String str4 = "TrY tO ReAd ThIs SenTEnCe".toUpperCase();
 
 System.out.println( "New Uppercase String = " + str1);
 System.out.println( "New Uppercase String = " + str2);
 System.out.println( "New Uppercase String = " + str3);
 System.out.println( "New Uppercase String = " + str4);
 }
}
Java toUpperCase method example

TIP: I suggest you refer to the Java string format and Java substring functions from the Java String Methods.

Java toUpperCase on empty string, numbers, and special characters

The toUpperCase function ignores everything except lowercase letters, including numbers, special characters, blank spaces, and empty strings. Therefore, if the toUpperCase() method finds any lowercase character in a given string, it transforms it to uppercase and leaves the remaining characters unchanged.

In the following example, you can see the string with only numbers or special characters are returned unchanged. However, if there is a combination of lowercase letters and numbers or special characters, the toUpperCase method converts the letters to uppercase.

public class example {
public static void main(String[] args)
{
System.out.println("".toUpperCase());
System.out.println("abc1234".toUpperCase());
System.out.println("Welcome@145*".toUpperCase());
System.out.println("22456".toUpperCase());
System.out.println("!&*@*".toUpperCase());

}
}
ABC1234
WELCOME@145*
22456
!&*@*

Use Java toUpperCase to convert the first letter of a string to uppercase?

Generally, we can use the capitalize() function to convert the first letter of a string to uppercase and the remaining characters to lowercase. However, from an interview point of view, if the task is to use toUpperCase, then use the program below.

In the following example,

  • st.substring(0, 1) returns the first character in a given string (p). Next, toUpperCase converts it to a capital P.
  • st.substring(1) returns the substring from the second character to the last character of the original string.
  • The Java arithmetic operator (+) will perform the string concatenation.
public class example {
public static void main(String[] args)
{
String st = "pizza and coke";
String old_order = st.toUpperCase();
String new_order = st.substring(0, 1).toUpperCase() + st.substring(1);

System.out.println(old_order);
System.out.println(new_order);
}
}
PIZZA AND COKE
Pizza and coke

Handle Java toUpperCase NullPointerException

The built-in String toUpperCase method accepts any non-null value for the uppercase conversion. If we pass a null value to convert it to uppercase, toUpperCase throws a NullPointerException.

public class example {
public static void main(String[] args)
{
String s = null;
System.out.println(s.toUpperCase());
}
}
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toUpperCase()" because "s" is null

Null-safe toUpperCase

To handle the NullPointerException, we can use either a Java if statement or the Java ternary operator to check for null. In the example below, we use the ternary operator to check whether the string is null or not. If it is null, return null; otherwise, use toUpperCase to convert the string characters to uppercase.

public class example {
public static void main(String[] args)
{
String s = null;
String res = (s == null)? s:s.toUpperCase();
System.out.println(res);
}
}
null

Why does Java toUppercase returns unexpected result with Turkish text?

The toUpperCase() function has an optional Locale argument that can be used to specify the locale language for uppercase conversion. In most cases, toUpperCase converts lowercase letters to uppercase. However, there are different meanings for Turkish text, so the result may be different. For example, Turkish text has both dotless and dotted I.

import java.util.Locale;

public class example {
public static void main(String[] args) {
String ch = "i";
System.out.println(ch.toUpperCase(Locale.ENGLISH));
System.out.println(ch.toUpperCase(Locale.forLanguageTag("tr-TR")));

String food = "pizza";
System.out.println(food.toUpperCase(Locale.ENGLISH));
System.out.println(food.toUpperCase(Locale.forLanguageTag("tr-TR")));

}
}
I
İ
PIZZA
PİZZA