String toUpperCase() method - Syntax and Examples
Learn about the Java String toUpperCase() method, its syntax, parameters, return value, and practical examples. This guide covers converting strings to uppercase in Java.
This reference explains String toUpperCase() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Introduction
The toUpperCase() method in Java is a handy tool for converting a string to uppercase. It's particularly useful when you need to standardize input or ensure consistency across different parts of your program. Let’s dive into how it works and explore some examples.
Syntax
public String toUpperCase()
public String toUpperCase(Locale locale)
Parameters
| Parameter | Description |
|---|---|
locale |
Optional. A Locale object that defines the rules for uppercase conversion based on a specific language or region. If omitted, the default locale of your system is used. |
Return Value
The toUpperCase() method returns a new string representing the uppercase version of the original string. The original string remains unchanged.
Examples
Example 1: Basic Uppercase Conversion
This example demonstrates how to convert a simple string to uppercase using the default locale.
String str = "hello world";
String upperCaseStr = str.toUpperCase();
System.out.println(upperCaseStr);
HELLO WORLD
Explanation: The original string "hello world" is converted to uppercase, resulting in the new string “HELLO WORLD”, which is then printed to the console.
Example 2: Using a Specific Locale
This example shows how to use a specific locale (Turkish) for uppercase conversion. Turkish has different rules for some characters compared to English.
import java.util.Locale;
String str = "merhaba dünya";
String upperCaseStr = str.toUpperCase(Locale.forLanguageTag("tr-TR"));
System.out.println(upperCaseStr);
MERHABA DÜNYA
Explanation: The string "merhaba dünya" is converted to uppercase using the Turkish locale (tr-TR). This ensures that any characters specific to Turkish are handled correctly during the conversion process. Note that in some locales, certain characters may not be uppercased.
Example 3: Uppercase with Special Characters
This example demonstrates how the method handles strings containing special characters and numbers.
String str = "Java123!@#";
String upperCaseStr = str.toUpperCase();
System.out.println(upperCaseStr);
JAVA123!@#
Explanation: The special characters and numbers remain unchanged as toUpperCase() only affects alphabetic characters.
Example 4: Handling Null Input
This example illustrates the behavior when passing a null string to the method. It's important to handle null values gracefully in your code.
String str = null;
String upperCaseStr = (str != null) ? str.toUpperCase() : ""; //Handle NullPointerException
System.out.println(upperCaseStr);
Explanation: Attempting to call toUpperCase() directly on a null string would result in a NullPointerException. To avoid this, we check if the string is not null before calling the method. If it's null, we assign an empty string as the default value.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.