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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/classes_objects -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/class_methods -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/modifiers -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/oop -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/wrapper_classes -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/identifier -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/arrays -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/comments -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/data_types -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/operators -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/output -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/strings -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/type_casting -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/variables -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/string_methods -.-> lab-117594{{"`Java Character toUpperCase Method`"}} java/system_methods -.-> lab-117594{{"`Java Character toUpperCase Method`"}} end

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