Java String toUpperCase() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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.

java
String code = country.toUpperCase(Locale.ROOT);   // import java.util.Locale
CallWhat it does
"java".toUpperCase(Locale.ROOT)"JAVA".
"v1.2-beta".toUpperCase(Locale.ROOT)"V1.2-BETA".
"stra\u00DFe".toUpperCase(Locale.ROOT)"STRASSE": one char becomes two.

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

ParameterDescription
localeOptional. The case-mapping rules to use. Without it, Locale.getDefault() is used.

Return Value

Return ValueDescription
StringThe 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);
    }
}

How It Works

  1. Letters only. Each lowercase letter becomes its capital; the colon and spaces are copied unchanged.
  2. Locale.ROOT for stable output. The i in "warning" becomes a plain I on every system, including Turkish ones.
  3. 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());
    }
}

How It Works

  1. An escape for the sharp s. \u00DF keeps the source ASCII while the string holds the German letter.
  2. No single capital exists. Unicode's standard mapping turns the sharp s into the two letters SS.
  3. 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);
    }
}

How It Works

  1. Store the new strings. Each array slot is replaced with the uppercase copy, because toUpperCase cannot change the old string.
  2. Join for display. String.join puts a space between IN, US, and GB.
  3. 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.

Next: transform()

Pass the string to a function and return its result (Java 12+).

transform() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful