The Java String toUpperCase() method converts all the lowercase letters of a string into upper case and returns that upper-cased string. It has the following general form.
Syntax:  
 1.  public String toUpperCase()
 2.  public String toUpperCase(Locale locale)
The second method converts all the characters of a string into uppercase using the rules of specified locale. 
Let's see a simple example program to understand the concept related to it with memory explanation.

Java String toUpperCase() method Example Programs


Program source code 1: 
    package stringPrograms; public class ToUpperCase1 { public static void main(String[] args) { String s=new String("technology"); String s2=s.toUpperCase(); System.out.println(s2); } }
    Output: TECHNOLOGY
Let's see first memory concept to understand the output of this program.
Memory Explanation: 
Java String toUpperCase() method
1. Whenever we create a string object using new operator, two objects will be created in the memory by JVM, one in the heap area and another in string constant pool. Because for every string literal one copy of an object is also created in the string constant pool for future purpose. Once the string object is created, JVM stores the content "technology" in it and pointing the reference variable s to that object.

2. Since the original content is in lowercase and we are calling toUpperCase() method to convert the content of s into uppercase. Means that here, we are trying to change the content of s. So, you know that once a string object is created, we are not allowed to perform any change in that content.
But if you try to change the content, a new object is created in the heap area by JVM and stores content "TECHNOLOGY" in it. Once the content is stored in heap area, JVM points the reference variable s2 to that object. Therefore, the output is "TECHNOLOGY".

As you can see in the above example program total of three objects are created in the heap area, two heap area and one in the string constant pool. 

Q. Consider the following program.
    package stringPrograms; public class ToUpperCase2 { public static void main(String[] args) { String s=new String("science"); // Line 4 String s2="science"; // Line 5 String s3=s.toUpperCase(); // Line 6 System.out.println(s==s2); // Line 7 System.out.println(s.equals(s2)); // Line 8 System.out.println(s.equalsIgnoreCase(s2)); // Line 9 System.out.println(s2==s3); // Line 10 System.out.println(s2.equals(s3)); // Line 11 } }
1. How many string objects will be created in the heap area and string constant pool?
2. What will be the output of the following program?

A. 1. Let's understand first memory concept of this program.
Java String toUpperCase() method
1. When the statement String s=new String("science"); will be executed by JVM, two string objects will form one in heap area and another in string constant pool.

2. When line 5 will be executed, JVM will not create a new object with content "science" in the string constant pool because it is already available in string constant pool. JVM will just refer the reference variable s2 to that object.

3. In line 6, we are trying to change the content of s by calling toUpperCase() method. So, whenever you will try to change the content of string, JVM will create a new object in the heap area and stores the content "SCIENCE" by converting the lowercase content into uppercase. The reference variable s3 will point to that new object in the heap area.


From the above memory diagram, it is clear that a total of 3 objects are created in the memory, two in heap area and one in scp area.

2. Line 7: The output will be false because double equal operator always compares references, not content and from the memory diagram, both reference variables s and s2 are pointing to the different objects, one in heap area and another in scp area.

Line 8: s.equals(s2); will give output true because equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.

Line 9: s.equalsIgnoreCase(s2); will also give output true.

Line 10: The output will be false because both reference variables are pointing to different objects.

Line 11: The output is false. This is because all characters are matched but the case is not the same.
    Output: false true true false false

Java String toUpperCase(Locale locale) Method Example Programs


Program source code 3:
    package stringPrograms; import java.util.Locale; public class ToUpperCase3 { public static void main(String[] args) { String s=new String("country"); String german = s.toUpperCase(Locale.forLanguageTag("gr")); String english = s.toUpperCase(Locale.forLanguageTag("en")); System.out.println(german); System.out.println(english); } }
    Output: COUNTRY COUNTRY
Final words 
Hope that this tutorial has covered all important points related to Java String toUpperCase() with example programs and memory explanation. I hope that you will have enjoyed this tutorial.
Thanks for reading!