What is Character.toUpperCase() in Java?

Overview

The toUpperCase() method is a static method in the Character class in Java that converts a character to uppercase.

The input for this function is a character.


Note: If you need to convert a string to uppercase, refer to the String.toUpperCase method.

Syntax


public static char toUpperCase(char ch)

Parameters

  • char ch - the character to convert to uppercase

Return value

This method returns the uppercase version of the input character.

Handling alphabetic characters

  • toUpperCase() converts a lowercase character to uppercase.

  • toUpperCase() converts a uppercase character to uppercase.

Code

In the code below, we use the toUpperCase function to convert non-alphabetic character to uppercase.

public class Main {
public static void main( String args[] ) {
char ch = 'a';
System.out.println(Character.toUpperCase(ch));
ch = 'A';
System.out.println(Character.toUpperCase(ch));
ch = 'z';
System.out.println(Character.toUpperCase(ch));
ch = 'Z';
System.out.println(Character.toUpperCase(ch));
}
}

Handling non-alphabetic characters

We take theexample of ?. Since it is not an alphabetic character, it remains unchanged.

Code

In the code below, we use the toUpperCase function to convert non-alphabetic character to uppercase.

public class Main {
public static void main( String args[] ) {
char nonAlphabeticChar = '?';
// Since '?' is not an alphabetic character, it remains unchanged
char unchangedChar = Character.toUpperCase(nonAlphabeticChar);
System.out.println("Uppercase of '" + nonAlphabeticChar + "' is: " + unchangedChar);
}
}

Free Resources