How to Convert a String to Upper Case in Java



Converting a string to upper case changes all characters in the string to their upper case equivalents. The toUpperCase() method in Java is used to convert all the strings into uppercase.

Converting a string into uppercase

The toUpperCase() method converts all the characters in a string into their upper case. This method returns a new string where every lowercase letter has been replaced by its uppercase equivalent. The following are the steps to convert a string into uppercase

  • Step 1. Declare a String: A string str is initialized with the value "string abc touppercase ".

  • Step 2. Convert to Uppercase: The toUpperCase() method is called on str to convert all characters in the string to uppercase. The result is stored in a new string strUpper.

Syntax

The following is the syntax for Java String toUpperCase() method
public String toUpperCase()
or,
public String toUpperCase(Locale locale)

Java program to convert a string into uppercase

The following is an example using the toUpperCase() method

public class StringToUpperCaseEmp {
   public static void main(String[] args) {
      String str = "string abc touppercase ";
      String strUpper = str.toUpperCase();
      System.out.println("Original String: " + str);
      System.out.println("String changed to upper case: " + strUpper);
   }
}

Output

Original String: string abc touppercase 
String changed to upper case: STRING ABC TOUPPERCASE

Code Explanation

The program utilizes the toUpperCase() method to convert an input string to uppercase. Initially, a string str is defined with the value "string abc touppercase". The toUpperCase() method is then called on this string, which converts all lowercase characters to uppercase. The transformed uppercase string is stored in a new variable, strUpper. This method returns a new string with the converted characters, while the original string remains unchanged.

java_strings.htm