Java String toUpperCase() method converts all the lowercase letters of a string into uppercase and returns that upper-cased string.
In other words, the toUpperCase() method of String class converts all the characters of a string into uppercase and returns that upper-cased string.
Non-alphabetical characters are unaffected by this method.
General Syntax of toUpperCase() method
This method comes into two general flavors that are as follows:
Syntax:
1. public String toUpperCase() 2. public String toUpperCase(Locale locale)
The first form of method converts all the characters of a string into uppercase using default locale. It does not accept any parameter.
The second form of method converts all the characters of a string into uppercase using the rules of specified locale.
Java String toUpperCase() method Example Program
Let’s take a simple example program in which we will convert all the characters of a string into uppercase letters and display the return uppercase string on the console.
Program code:
package stringProgram; public class ChangeCase { public static void main(String[] args) { // Create a string. String s = new String("technology"); System.out.println("Original string: " +s); // Convert all characters of string into uppercase letters using toUpperCase() method. String s2 = s.toUpperCase(); System.out.println("Uppercase letters: " +s2); } }
Output: Original string: technology Uppercase letters: TECHNOLOGY
Let’s see the memory concept to understand the output of this program.
Memory Explanation:
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.
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.
As we know that once a string object is created, we are not allowed to perform any change in that content.
But if we 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 stringProgram; public class ChangeCase { public static void main(String[] args) { String s = new String("science"); // Line 5 String s2 = "science"; // Line 6 String s3 = s.toUpperCase(); // Line 7 System.out.println(s==s2); // Line 8 System.out.println(s.equals(s2)); // Line 9 System.out.println(s.equalsIgnoreCase(s2)); // Line 10 System.out.println(s2==s3); // Line 11 System.out.println(s2.equals(s3)); // Line 12 } }
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?
Answer 1:
Let’s understand the memory concept of this program first. Look at the below figure and understand explanation.
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 6 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 7, we are trying to change the content of s by calling toUpperCase() method. So, whenever we 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.
Answer 2:
Line 8: The output will be false because double equal operator always compares references, not contents. 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 9: 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.
Similarly, in the line 10: s.equalsIgnoreCase(s2); will also give output true.
Line 11: The output will be false because both reference variables are pointing to different objects.
Line 12: The output is false. This is because all characters are matched but the case is not the same.
Output: false true true false false
String toUpperCase(Locale locale) Method Example Program
Let’s take another example program where we will convert all the characters of a string into uppercase letters using specified locale as a parameter in toUpperCase() method.
Program code:
package stringProgram; import java.util.Locale; public class ChangeCase { public static void main(String[ ] args) { String s = new String("country"); // Converting all charaters of string into uppercase letters. 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
Hope that this tutorial has covered all important points related to Java String toUpperCase() method with example programs. I hope you will have understood the memory concepts of all the example programs.
In the next tutorial, we will learn how to get characters from string. If you get anything incorrect in this tutorial, then please inform to our team through email.
Thanks for reading!!!
Next ⇒ How to get Character from String in Java⇐ Prev Next ⇒