Java Character toUpperCase Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the toUpperCase() method of the Character class in Java. The toUpperCase() method is used to convert the given Unicode code point character argument to uppercase using the case mapping information provided by the Unicode data file.

Create a new Java file

Navigate to the ~/project directory and create a new Java file named CharacterCaseConversion.java using the below command:

touch CharacterCaseConversion.java

Define the class and main method

Add the below code to define the class and main method to the CharacterCaseConversion.java file. The main method is the entry point of the program.

public class CharacterCaseConversion{
   public static void main(String[] args){
      // write code for method testing here
   }
}

Convert character to the uppercase

Add the below code to convert a character to its uppercase equivalent using toUpperCase() method. We will use Character.toUpperCase(int codePoint) where codePoint is the Unicode code point character value to be converted.

char ch = 'a';
int cp = (int) ch; //convert char to codepoint
int uc = Character.toUpperCase(cp);
char uch = (char) uc;
System.out.println("The uppercase of '"+ch+"' is '"+uch+"'");

This code will convert the character 'a' to its uppercase equivalent 'A' and print it.

Test your code

Compile and run the program using the below command to test your code.

javac CharacterCaseConversion.java && java CharacterCaseConversion

Convert string to uppercase

Add the below code snippet to convert the entire string to its uppercase equivalent using toUpperCase() method. We will use String.toUpperCase() method to convert every character of the string to its uppercase equivalent.

String str = "Hello World";
String upStr = str.toUpperCase();
System.out.println("The uppercase string is: "+upStr);

This code will convert the original string "Hello World" to its uppercase equivalent "HELLO WORLD" and print it.

Test your code

Compile and run the program using the below command to test your code.

javac CharacterCaseConversion.java && java CharacterCaseConversion

Convert string to uppercase at specific index

Add the below code snippet to convert the specific character of the string to its uppercase equivalent using toUpperCase() method. We will use Character.toUpperCase(char c) method to convert the character at specific index to its uppercase equivalent.

String str2 = "Meet me at 12 o'clock";
char[] arr = str2.toCharArray();
int index = 5;
arr[index] = Character.toUpperCase(arr[index]);
str2 = new String(arr);
System.out.println("The modified string is: "+str2);

This code will modify the original string "Meet me at 12 o'clock" by converting the character 'm' at index 5 to its uppercase equivalent 'M' and print the modified string: "Meet Me at 12 o'clock".

Test your code

Compile and run the program using the below command to test your code.

javac CharacterCaseConversion.java && java CharacterCaseConversion

Convert string to lowercase

Add the below code snippet to convert the entire string to its lowercase equivalent using toLowerCase() method. We will use String.toLowerCase() method to convert every character of the string to its lowercase equivalent.

String str = "Hello World";
String loStr = str.toLowerCase();
System.out.println("The lowercase string is: "+loStr);

This code will convert the original string "Hello World" to its lowercase equivalent "hello world" and print it.

Test your code

Compile and run the program using the below command to test your code.

javac CharacterCaseConversion.java && java CharacterCaseConversion

Summary

In this lab, you learned how to use the toUpperCase() method of the Character class to convert characters and strings to their uppercase equivalents, as well as how to use the toLowerCase() method to convert characters and strings to their lowercase equivalents.

Other Java Tutorials you may like