The Java String toUpperCase() returns a string which is result of converting all characters in given string to UPPERCASE.
Read more: Java example capitalize the string
1. String toUpperCase() method
Use String.toUpperCase()
converts any string to UPPECASE letters.
1.1. Method syntax
String toUpperCase()
method is overloaded and comes in two variants.
/** * @param locale - locale use the case transformation rules for given locale * * @return - string converted to uppercase */ public String toUpperCase(); public String toUpperCase(Locale locale);
1.2. ‘null’ is not valid method argument
Method does not accept 'null'
argument. It will throw NullPointerException
in case method argument is null.
Exception in thread "main" java.lang.NullPointerException at java.lang.String.toUpperCase(String.java:2710) at com.StringExample.main(StringExample.java:11)
2. Java convert string to uppercase example
Java program to convert the string to uppercase using default locale rules.
public class StringExample { public static void main(String[] args) { String string = "hello world"; String uppercaseString = string.toUpperCase(); System.out.println(uppercaseString); } }
Program output.
HELLO WORLD
toUpperCase()
method is equal to callingtoUpperCase(Locale.getDefault())
.
3. Java String toUpperCase(Locale locale) example
Java program to convert the string to uppercase using default locale rules.
public class StringExample { public static void main(String[] args) { System.out.println("hello world".toUpperCase(Locale.getDefault())); System.out.println("Γειά σου Κόσμε".toUpperCase(Locale.US)); } }
Program output.
HELLO WORLD ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ
In this example, we learned to convert string to uppercase.
References:
A Guide to Java String
String Java Doc