Java Character toUpperCase() example

In this guide, you will learn about the Character toUpperCase() method in Java programming and how to use it with an example.

1. Character toUpperCase() Method Overview

Definition:

The toUpperCase() method from the Java Character class converts the character argument to uppercase using case mapping information from the Unicode data file. Like its counterpart toLowerCase(), this method does not consider the locale of the user.

Syntax:

1. static char toUpperCase(char ch) 
2. static int toUpperCase(int codePoint)

Parameters:

char ch: The character to be converted.

int codePoint: The character (Unicode code point) to be converted.

Key Points:

- This method is specialized for converting individual characters, not whole strings. For string conversion, it's recommended to use String.toUpperCase().

- The method will return the input character if it's already in uppercase or if it doesn't have an uppercase equivalent.

- The conversion is strictly based on the Unicode standard, meaning it may not be suitable for all linguistic contexts since it does not take the locale into account.

2. Character toUpperCase() Method Example

public class ToUpperCaseExample {
    public static void main(String[] args) {
        char testChar1 = 'a';
        char testChar2 = 'A';
        int testCodePoint = 0x00DF;  // Latin Small Letter Sharp S

        System.out.println("Uppercase of '" + testChar1 + "' is: " + Character.toUpperCase(testChar1));
        System.out.println("Uppercase of '" + testChar2 + "' is: " + Character.toUpperCase(testChar2));
        System.out.println("Uppercase of Latin Small Letter Sharp S is: " + (char)Character.toUpperCase(testCodePoint));
    }
}

Output:

Uppercase of 'a' is: A
Uppercase of 'A' is: A
Uppercase of Latin Small Letter Sharp S is: ß

Explanation:

In the provided example, the toUpperCase() method is applied to a lowercase letter 'a', an uppercase letter 'A', and a Unicode character 'Latin Small Letter Sharp S'. 

For 'a', the function converts and returns the uppercase 'A'. 

For the already uppercase 'A', it simply returns 'A' again. 

For the 'Latin Small Letter Sharp S', there's no direct uppercase equivalent in the Unicode standard, so it just returns the input character itself.

Comments