Java is a case-sensitive language, for example, it considers “java” and “JAVA” as two different Strings. To perform operations like comparison among case-insensitive data, it is required to first convert them in the same case format such as uppercase. This conversion of data to the uppercase format is done via the “toUpperCase()” method of the “java.util” package. This method also avoids any potential issues related to case sensitivity during searching.

This guide explains the below-mentioned sections:

  • How to Use the String toUpperCase() Method in Java?
  • Internal Implementation of toUpperCase() Method
  • Java Character.toUpperCase() Method
  • Conclusion

How to Use the String toUpperCase() Method in Java?

The Java String “toUpperCase()” method converts the String characters cases to uppercase. This method can be applied on the String or single character as required. The conversion to uppercase is performed under the default “Locale”. However, users can also provide any other Locale parameter to perform conversion accordingly.

Syntax

The syntax of a String “toUpperCase()” method with and without “Locale” parameters are stated below:

//For Default Locale Casing Rules
public String toUpperCase()

//For Specific Locale Casing Rules
public String toUpperCase(Locale locale)

Let’s dive into the implementation of the String “toUpperCase()” method by covering the mentioned examples.

Example 1: Use of String “toUpperCase()” Method in Java

In this example, the “toUpperCase()” method is applied over multiple Strings to set their cases into uppercase:

class javabeat{
public static void main(String args[]) {//Defining the main() driver method
  String str1 = "Learn Java With JavaBeat";
  String str2 = "hello java";
  String str3 = "123Java@Beat456";//Conversion to UpperCase Format
  String convertStr1 = str1.toUpperCase();
  String convertStr2 = str2.toUpperCase();
  String convertStr3 = str3.toUpperCase();
  System.out.println("\nThe String '" + str1 + "' is converted to Uppercase as: \n" + convertStr1);
  System.out.println("\nThe String '" + str2 + "' is converted to Uppercase as: \n" + convertStr2);
  System.out.println("\nThe String '" + str3 + "' is converted to Uppercase as: \n" + convertStr3);
}
}

The description of the above code block is as follows:

  • First, the class named “javabeat” having the “main()” method is created.
  • Next, the three targeted String-type variables are declared and initialized. The “toUpperCase()” method is applied on each variable, to convert their cases into uppercase.
  • After that, the results are stored in the “convertStr1”, “convertStr2”, and “convertStr3” variables respectively.
  • Finally, these variables are displayed on the console for better visual representation. 

The output confirms that the provided Strings are converted into Uppercase format:

Example 2: Comparing Case In-Sensitive Strings Using String toUpperCase() Method

In this example, the given Strings are first converted into the uppercase format and then passed to the “equals()” method for comparison. The code for a discussed scenario is shown below:

class javabeat{
public static void main(String args[]) {
String str1 = "Learn Java";
String str2 = "LEARN JAVA";
String str3 = "WITH JAVABEAT";

//Convert String Case Into Upper case
  String convertStr1 = str1.toUpperCase();
  String convertStr2 = str2.toUpperCase();
  String convertStr3 = str3.toUpperCase();

//Comparing the Upper case String
  boolean result1 = convertStr1.equals(convertStr2);
  boolean result2 = convertStr2.equals(convertStr3);

//Displaying the Comparison Result
  System.out.println("\nThe comparison results for '" + str1 + "' and '" + str2 + "' Strings is: \n" + result1);
  System.out.println("\nThe comparison results for '" + str2 + "' and '" + str3 + "' Strings is: \n" + result2);
}
}

The explanation of the above code snippet is as follows:

  • First, a class and “main()” method which contains three String type variables namely “str1”, “str2”, and “str3” are created.
  • Next, the “toUpperCase()” method is applied to these Strings. The generated results are stored in String type variables “convertStr1”, “convertStr2”, and “convertStr3”.
  • The “convertStr1” and “convertStr2” are passed in the “.equals()” method to check whether both Strings are equal or not. 
  • The result is stored in the boolean type variable named “result1”. If the returned result is “true” means both Strings are equal and vice versa in the case of “false”.
  • The same process is done for “convertStr2”, and “convertStr3” variables and their result is stored in the “result2” variable.

The output for the above code shows that comparison is done for case-insensitive Strings:

Example 3: Using String toUpperCase() Method With Locale Parameter

The String “toUpperCase()” method can accept a single optional parameter of “Locale”. This parameter contains details about the language according to which the conversion of String into uppercase needs to be performed. The implementation of a “toUpperCase()” method with the discussed parameter is shown below:

import java.util.Locale;
class javabeat {
public static void main(String args[]) {
  String str1 = "Learn Java With JavaBeat";
//Assigning Specific Locales
  Locale Lithuanian = Locale.forLanguageTag("lt");
  Locale ENGLISH = Locale.forLanguageTag("en");
//Conversion to uppercase according to the specified Locales
  String convertStr1 = str1.toUpperCase(Lithuanian);
  String convertStr2 = str1.toUpperCase(ENGLISH);
  System.out.println("\nThe Uppercase version in '" + Lithuanian + "' Locale for '" + str1 + "' is: \n" + convertStr1);
  System.out.println("\nThe Uppercase version in '" + ENGLISH + "' Locale for '" + str1 + "' is: \n" + convertStr2);
}
}

The explanation of the above code snippet is shown below:

  • First, the String type variable “str1” containing a dummy value is declared inside the “main()” method. 
  • Then, two instances of the “Locale” object are created using the “forLanguageTag()” method. 
  • The first one is for the “Lithuanian” language, and the other is for the “English” language. They each have their tags and secret codes for easy identification.
  • Next, these Locale objects are converted into an uppercase format using the “toUpperCase()” method.
  • The result is stored in separate variables which are then displayed on the console.

The output shows the converted values in uppercase format according to the provided Locale values:

Internal Implementation of toUpperCase() Method

The working or internal implementation of the “toUpperCase()” method in Java language is shown below:

public String toUpperCase(Locale locData) {
if (locData == null) {
  throw new NullPointerException();
}
int firstLowerChar;
final int stringLength = val.length;
scan: {
  for (firstLowerChar = 0 ; firstLowerChar < stringLength; ) {
  int z = (int)val[firstLowerChar];
  int stringChar;
  if ((z >= Character.MIN_HIGH_SURROGATE) && (z <= Character.MAX_HIGH_SURROGATE))       {
    z = codePointAt(firstLowerChar);
    stringChar = Character.charCount(z);
    } else {
    stringChar = 1;
    }
    int upperCaseChar = Character.toUpperCaseEx(z);
    if ((upperCaseChar == Character.ERROR) || (z != upperCaseChar)) {
    break scan;
    }
    firstLowerChar += stringChar;
  }
  return this;
  }
  char[] output = new char[stringLength];
  int outputOffset = 0; 

  System.arraycopy(val, 0, output, 0, firstLowerChar);
  String localeLang = locData.getLanguage();
  boolean localeDetail =     (localeLang == "tr" || localeLang == "az" || localeLang == "lt");
  char[] modifiedUpperArray;
  int upperChar;
  int selectedChar;
  int stringChar;
    for (int i = firstLowerChar; i < stringLength; i += stringChar) {
  selectedChar = (int)val[i];
  if ((char)selectedChar >= Character.MIN_HIGH_SURROGATE &&
    (char)selectedChar <= Character.MAX_HIGH_SURROGATE) {
    selectedChar = codePointAt(i);
    stringChar = Character.charCount(selectedChar);
  } else {
    stringChar = 1;
  }
  if (localeDetail) {
  upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locData);
  } else {
  upperChar = Character.toUpperCaseEx(selectedChar);
}
if ((upperChar == Character.ERROR)  || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
  if (upperChar == Character.ERROR) {
  if (localeDetail) {
    modifiedUpperArray =
    ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locData);
  } else {
    modifiedUpperArray = Character.toUpperCaseCharArray(selectedChar);
  }
} else if (stringChar == 2) {
outputOffset += Character.toChars(upperChar, output, i + outputOffset) - stringChar;
continue;
} else {
  modifiedUpperArray = Character.toChars(upperChar);}

The working of the above code block is stated below:

  • First, the “toUpperCase()” method takes the “Locale” object as a parameter. This parameter tells how a String needs to be converted based on provided language-specific rules.
  • To prevent the “NullPointerException()”, the “Locale” object is checked for a “null” value using the conditional “if” statement.
  • Next, the first character from the provided String is identified by traversing through the String using the “for” loop. The special characters are also considered if they are available in the String.
  • Then, the provided Locale rules are analyzed whether that character needs to be converted under custom Locale rules or the default rules.
  • After that, the conversion is performed on that character. The result is stored in an array which gets returned as an output when the entire String gets converted.

Bonus Tip: Java Character.toUpperCase() Method

The “toUpperCase(char ch)” method can be used with a single targeted character to set its formatting to uppercase. The targeted character is passed to the “toUpperCase()” method as a parameter. The specified method then returns the same character but in the uppercase format. Instead of single characters Unicode or ASCII code values can also be passed to set their corresponding String characters into uppercase. 

Syntax

The syntax for “Character.toUpperCase()” method is shown below:

//For character "char" type values:
public static char toUpperCase(char ch) 

//For codepoints "int" type values:
public static int toUpperCase(int codePoint)

Let’s take a look at below code snippet for the demonstration of a “Character.toUpperCase()” method:

class javabeat {
public static void main(String args[]) {//Declaring and Initializing the targeted variables
  char provChar1, provChar2;
  provChar1 = 'j';
  provChar2 = 'a';
 
  //Converting Characters into Uppercase
  char convertedStr1 = Character.toUpperCase(provChar1);
  char convertedStr2 = Character.toUpperCase(provChar2);

  //Displaying Converted Characters
  System.out.println("\nThe Uppercase Character version of '" + provChar1 + "' is: \n" + convertedStr1);
  System.out.println("\nThe Uppercase Character version of '" + provChar2 + "' is: \n" + convertedStr2);
}
}

The description of the above code is as follows:

  • First, the class “javabeat” is created along with the “main()” method. In the method, two “char” type variables namely “provChar1” and “provChar2” are declared and initialized.
  • Next, these created variables are passed as parameters in the “Character.toUpperCase()” method. The returned result is stored in the “convertedStr1” and “convertedStr2” variables respectively.
  • In the end, the value or result stored in the earlier created variables is displayed for verification purposes.

The output shows that the provided single character is converted into uppercase:

That’s all about the usage of a String “toUpperCase() method in Java. 

Conclusion

In Java, the “toUpperCase()” method converts the case of String characters into upper case format with the default Locale. This method depends on the default locale for casing rules unless a specific Locale is provided as a parameter. Users can use this method to retrieve the data in the same format, case-insensitive string comparisons, and in many more scenarios. This guide has explained the usage and working of the String “toUpperCase() method in Java.