In Java, toUpperCase(locale) returns a new String with every character converted to uppercase using the given locale's rules. The original string is never changed.
Typical uses are headings, status codes, and normalized keys such as country codes. Pass Locale.ROOT or Locale.ENGLISH so that machine-readable text is converted the same way on every computer.
toUpperCase() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.toUpperCase(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result.
Syntax
Pass a locale explicitly; the overload without arguments uses the JVM default locale.
The Turkish pitfall: in a Turkish locale, lowercase i uppercases to a capital I with a dot (U+0130). A check like cmd.toUpperCase().equals("EXIT") then fails for "exit" on a Turkish system. With Locale.ROOT, i always becomes I.
Library declarations
java
public String toUpperCase();
public String toUpperCase(Locale locale);
Parameters
Parameter
Description
locale
Optional. The case-mapping rules to use. Without it, Locale.getDefault() is used.
Return Value
Return Value
Description
String
The uppercase text, which can be longer than the input.
Java Examples
Example 1 — Convert a Message to Uppercase
Turn a warning into capitals and keep the original text.
java
import java.util.Locale;
public class ToUpperCaseBasic {
public static void main(String[] args) {
String message = "warning: disk almost full";
String loud = message.toUpperCase(Locale.ROOT);
System.out.println(loud);
System.out.println(message);
}
}
Output
WARNING: DISK ALMOST FULL
warning: disk almost full
How It Works
Letters only. Each lowercase letter becomes its capital; the colon and spaces are copied unchanged.
Locale.ROOT for stable output. The i in "warning" becomes a plain I on every system, including Turkish ones.
Two separate strings.message still holds the lowercase text, because the method returned a new String.
Example 2 — The German Sharp s Becomes SS
Uppercasing can make a string longer.
java
import java.util.Locale;
public class ToUpperCaseSharpS {
public static void main(String[] args) {
String street = "stra\u00DFe";
String upper = street.toUpperCase(Locale.ROOT);
System.out.println(upper);
System.out.println("length before: " + street.length());
System.out.println("length after: " + upper.length());
}
}
Output
STRASSE
length before: 6
length after: 7
How It Works
An escape for the sharp s.\u00DF keeps the source ASCII while the string holds the German letter.
No single capital exists. Unicode's standard mapping turns the sharp s into the two letters SS.
Lengths differ. Six chars become seven, so code must not assume upper.length() == street.length().
Example 3 — Normalize Codes and Capitalize a Name
Uppercase whole codes, or only the first letter of a name.
java
import java.util.Locale;
public class ToUpperCaseCapitalize {
public static void main(String[] args) {
String[] codes = {"in", "Us", "gB"};
for (int i = 0; i < codes.length; i++) {
codes[i] = codes[i].toUpperCase(Locale.ENGLISH);
}
System.out.println(String.join(" ", codes));
String name = "ada";
String capitalized = name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1);
System.out.println(capitalized);
}
}
Output
IN US GB
Ada
How It Works
Store the new strings. Each array slot is replaced with the uppercase copy, because toUpperCase cannot change the old string.
Join for display.String.join puts a space between IN, US, and GB.
Capitalize just the first letter. Uppercase the one-char substring(0, 1) and append the rest from index 1, giving "Ada".
Frequently Asked Questions
The no-argument version uses the default locale. In Turkish, i becomes a dotted capital I (U+0130), which breaks comparisons with "I". Locale.ROOT avoids that.
No. It returns a new String; the original stays unchanged. Assign the result to use it.
The German sharp s has no single-letter capital in the standard mapping, so it becomes "SS" and the result has one more char.
Combine substring and toUpperCase: s.substring(0, 1).toUpperCase(Locale.ROOT) + s.substring(1), after checking that s is not empty.
No. Only characters that have an uppercase form are converted; digits, spaces, and punctuation are copied as they are.