What is toUpperCase() Method?
Example
Let’s have a look at a basic example to convert a string to ALL-CAPS using the toUpperCase() method in java.
public class UpperCase {
public static void main(String[] args) {
String myCountry = "pakistan";
System.out.println("My Country name is: " + myCountry);
// convert a string to uppercase
System.out.println("My Country name using toUpperCase() is: " + myCountry.toUpperCase() + "\n");
String paragraph = "Code Gym is an amazing place to start your java learning journey!";
System.out.println("Text paragraph: " + paragraph);
// convert a paragraph to uppercase
System.out.println("Text paragraph after using toUpperCase() method: " + paragraph.toUpperCase() + "\n");
char dummyChar = 'a';
System.out.println("dummyChar is: " + dummyChar);
// convert the primitive 'char' type to uppercase
System.out.println("dummyChar using toUpperCase() is: " + Character.toUpperCase(dummyChar));
}
}
Output
toUpperCase(Locale locale) for Internationalization
You've mastered the basic toUpperCase()
method that converts strings to uppercase based on your default locale, but what if you’re dealing with text for users in different countries? This is where toUpperCase(Locale locale)
shines. Let's see how it works, why it matters, and what pitfalls to watch out for!
Purpose of toUpperCase(Locale locale)
By default, toUpperCase()
uses your system or JVM’s default locale, which might be just fine if you only handle one language. But if you’re building an international application, you need precise control. toUpperCase(Locale locale)
lets you specify the exact locale for your conversion. That means if you have text in Turkish, for example, you can pass new Locale("tr")
to handle special letters like ‘ı’ or ‘İ’ properly. Talk about being globally friendly, right?
String turkishString = "istanbul";
String upperTurkish = turkishString.toUpperCase(new Locale("tr"));
System.out.println(upperTurkish); // ISTANBUL (locale-specific magic!)
The Default Locale and Why It Matters
If you just do myString.toUpperCase()
without specifying a locale, Java uses the default locale of your system or JVM. That might produce unexpected results if your environment is set to a locale that differs from your user’s language. For most simple use cases (like English text), you might never notice the difference. But for more complex scripts or languages with special casing rules, you really want to be explicit. Otherwise, you can end up with weird behavior or incorrectly capitalized letters.
So if your user base spans different regions, specifying the locale is a good practice—it ensures consistent results, no matter where the code runs or what the system’s default locale is.
Potential Errors and Exceptions
If you decide to use toUpperCase(Locale locale)
, be mindful of possible pitfalls. One common error is passing in a null
locale. That’ll throw a NullPointerException
, stopping your code right in its tracks. Always ensure you have a valid Locale
object, like Locale.US
, Locale.FRANCE
, or a custom one like new Locale("tr", "TR")
for Turkish in Turkey.
try {
String input = "Çalışma";
String result = input.toUpperCase(null); // This will throw NullPointerException!
} catch (NullPointerException e) {
System.out.println("Oops, locale was null. " + e);
}
To avoid this, double-check your locale is never null
, and consider defaulting to something explicit, like Locale.ENGLISH
, if you’re uncertain.
GO TO FULL VERSION