Java String toUpperCase() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
When working with text in Java, you often need to change the way letters appear, like making them all uppercase. The Java String class has a helpful method called toUpperCase() for this purpose. It takes the letters in a given string and turns them into their uppercase form, following the rules of the language used or the one you specify.
In this article, we’ll take a closer look at the toUpperCase() method in Java. We’ll cover its basic usage, what you can put inside it, what it gives you back, and show you some practical examples of how it can be used.
Whether you’re an experienced Java programmer wanting a refresher or new to Java and eager to learn, this article will help you understand how to manipulate text in Java more effectively. Let’s get started and explore the toUpperCase() method together.
Overview of Java toUpperCase() Method
The toUpperCase() method in Java is used to convert all the characters in a string to their uppercase counterparts. It is essential for tasks like case-insensitive string comparison or ensuring consistent capitalization in your text.
There are two variants of this method:
- public String toUpperCase(Locale loc): This variant takes a Locale object as a parameter and converts all characters to uppercase according to the rules of the specified locale.
- public String toUpperCase(): This variant converts all characters to uppercase using the default locale.
It’s worth noting that the toUpperCase() method follows the rules of the given locale for converting lowercase characters to uppercase, which is particularly important when dealing with languages that have special case rules, such as Turkish.
Syntax of toUpperCase in Java
The toUpperCase() method in Java has two method signatures:
1. public String toUpperCase(Locale loc)
This variant accepts a Locale object as a parameter and returns a new string with all characters converted to uppercase according to the rules of the specified locale.
2. public String toUpperCase()
This variant has no additional parameters and converts all characters to uppercase using the default locale.
Parameters
Let’s take a closer look at the parameters for each variant of the toUpperCase() method:
Type 1: public String toUpperCase(Locale loc)
Locale loc: This is the locale that defines the rules for converting characters to uppercase. The method will use the rules of the provided locale to perform the conversion. The locale can be specific to a language, region, and culture.
Type 2: public String toUpperCase()
This variant of the method doesn’t require any additional parameters. It simply converts all characters in the string to uppercase using the default locale.
Return Type
The toUpperCase() method returns a new string with all characters converted to uppercase letters. The important thing to note is that the conversion follows the rules of the given locale.
Example of toUpperCase in Java
Example 1:
public class ToUpperCaseExample {
public static void main(String[] args) {
String input = "Hello, World!";
String upperCaseString = input.toUpperCase();
System.out.println("Original String: " + input);
System.out.println("Uppercase String: " + upperCaseString);
}
}Output:
Original String: Hello, World!
Uppercase String: HELLO, WORLD!
Example 2:
import java.util.Locale;
public class ToUpperCaseExample {
public static void main(String[] args) {
// Create a Locale for the Turkish language, where 'i' has a special uppercase form.
Locale turkishLocale = new Locale("tr", "TR");
// A string with lowercase 'i' with a dot, such as "i̇stanbul i̇zmir."
String text = "i̇stanbul i̇zmir";
// Convert the string to uppercase using the Turkish locale.
String uppercaseText = text.toUpperCase(turkishLocale);
// Display the original and uppercase strings.
System.out.println("Original Text: " + text);
System.out.println("Uppercase Text: " + uppercaseText);
}
}Output:
Original Text: i̇stanbul i̇zmir
Uppercase Text: İSTANBUL İZMIR
Conclusion
In conclusion, the toUpperCase() method in Java is a valuable tool for manipulating strings by converting all characters to their uppercase equivalents. It offers two variants, one that follows the rules of a specified locale and another that uses the default locale for the conversion. This method is particularly useful for tasks like case-insensitive string comparison and ensuring consistent capitalization in text, especially when dealing with languages with unique case rules.
We have explored the syntax, parameters, and return type of the toUpperCase() method, providing a comprehensive understanding of its functionality. Additionally, we have demonstrated practical examples of its usage, showcasing its versatility in different scenarios, including working with locales with specific case rules.
By incorporating the toUpperCase() method into your Java code, you can efficiently handle string transformations and enhance the robustness of your text-processing applications. Whether it’s for standard text manipulation or locale-specific requirements, this method proves to be a valuable asset in your Java programming toolbox.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

