CodeGym /Java Blog /Strings in Java /Java toUpperCase() Method
Author
Vasyl Malik
Senior Java Developer at CodeGym

Java toUpperCase() Method

Published in the Strings in Java group

What is toUpperCase() Method?

“The toUpperCase() method in Java returns the input content in ALL CAPS.”
Java provides you with a simple, easy to use and quite helpful function to convert any text, characters or strings in ALL-CAPS. There are a lot of times when you have to compare the content of a string or a paragraph, irrespective of the case sensitivity. In those situations, the java method toUpperCase() can be freely used to first convert all the content in CAPS and then simply check if it’s equal.

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
My Country name is: pakistan My Country name using toUpperCase() is: PAKISTAN Text paragraph: Code Gym is an amazing place to start your java learning journey! Text paragraph after using toUpperCase() method: CODE GYM IS AN AMAZING PLACE TO START YOUR JAVA LEARNING JOURNEY! dummyChar is: a dummyChar using toUpperCase() is: A

Conclusion

So that was a precise post on how you can use the java toUpperCase() method with strings, text paragraphs or a primitive char. Don’t forget to practise as much, and come back to this article whenever you feel the need to. Happy coding!
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
DarthGizka Level 24, Wittenberg, Germany
10 June 2021
Note: in a Western European context it can be advantageous to use toLowerCase() instead of toUpperCase() if this is intended as some kind of case normalisation (e.g. doing exact lookups on case-normalised strings instead of doing case-insensitive lookups on non-normalised strings). Conversion to lower case tends to have somewhat fewer problems than conversion to upper case in such situations. For example, there is no upper case 'ß' in the German language and so Java's toUpperCase() turns it into 'SS' (!) - which might not be quite what you expect. However, even with toLowerCase() you have to stay away from some Turkish characters... In general we expect the upper case version of 'i' to be 'I' but in Turkey it's 'i' and 'İ' (both dotted) vs 'ı' and 'I' (neither dotted). It is much more logical that way but probably not quite what most of us are used to... 🤠