Java String toUpperCase()

The java string toUpperCase() method converts all characters of the string into a uppercase letter. There are two variant of toUpperCase() method.

Java String to UpperCase
  • Java String toUpperCase() method has two variants – toUpperCase() and toUpperCase(Locale locale).

  • Conversion of the characters to uppercase is done by using the rules of default locale. Calling toUpperCase() function is same as calling toUpperCase(Locale.getDefault()).

  • Java String to upper case method is locale sensitive, so use it carefully with strings that are intended to be used locale independent. For example programming language identifiers, HTML tags, protocol keys etc. Otherwise you might get unwanted results.

  • To get correct upper case results forlocale insensitive strings, use toUpperCase(Locale.ROOT) method.

  • String toUpperCase() returns a new string, so you will have to assign that to another string. The original string remains unchanged because String is immutable.

  • If locale is passed as null to toUpperCase(Locale locale) method, then it will throw NullPointerException.

Signature

There are two variant of toUpperCase() method. The signature or syntax of string toUpperCase() method is given below:

public String toUpperCase() public String toUpperCase(Locale locale)

The second method variant of toUpperCase(), converts all the characters into uppercase using the rules of given Locale.

Returns

string in uppercase letter.

Java String toUpperCase() method example
public class Test { public static void main(String args[]) { String str1="hello cprogramcoding"; String upper=str1.toUpperCase(); System.out.println(upper); } }


Output:
HELLO CPROGRAMCODING
Java String toUpperCase() method example
// Java program to demonstrate // working of toUpperCase() method class Test { public static void main(String args[]) { String str = "Welcome! to cprogramcoding Planet."; // converting string str to uppercase letter String p = str.toUpperCase(); System.out.println(p); } }


Output:
WELCOME! TO CPROGRAMCODING PLANET.
Java String toUpperCase(Locale locale) Method Example
import java.util.Locale; pubpublic class Test { public static void main(String[] args) { String s = "hello cprogramcoding"; String turkish = s.toUpperCase(Locale.forLanguageTag("tr")); String english = s.toUpperCase(Locale.forLanguageTag("en")); System.out.println(turkish); System.out.println(english); }


Output:
HELLO CPROGRAMCOD?NG
Java String toUpperCase(Locale locale) Method Example
// Java program to demonstrate // working of Locale class in // toUpperCase() method import java.util.Locale; class Test { public static void main(String args[]) { String str = "Learn Java in simple and easy steps starting from basic to advanced concepts."; // Locales with the language "tr" for TURKISH //"en" for ENGLISH is created Locale TURKISH = Locale.forLanguageTag("tr"); Locale ENGLISH = Locale.forLanguageTag("en"); // converting string str to uppercase letter // using TURKISH and ENGLISH language String p2 = str.toUpperCase(TURKISH); String p3 = str.toUpperCase(ENGLISH); System.out.println(p2); System.out.println(p3); } }


Output:
LEARN JAVA ?N S?MPLE AND EASY STEPS START?NG FROM BASIC TO ADVANCED CONCEPTS. LEARN JAVA IN SIMPLE AND EASY STEPS STARTING FROM BASIC TO ADVANCED CONCEPTS.



Instagram