Java

Uppercase and Lowercase Functions in Java

This article explains Uppercase and Lowercase Functions in Java with examples.

In order to carry out the conversion of lowercase letters to uppercase and vice versa, there are two functions in Java. These functions are toUpperCase() and toLowerCase(). The following section explains these functions.

A Program to Demonstrate Uppercase and Lowercase Functions in Java

The following program declares a character variable input and initializes it with a character value. Also, a String type variable called output is also declared and initialized with a blank string. Since we need to call the methods toUpperCase(), and toLowerCase() with a String object, so we need to convert the variable input to String. Therefore, here we use the toString() method of the Character class to convert the variable input to String and assign it to the String object s1.

The toString() Method of the Character Class

Signature: public static String toString(char my_char)

Basically, the toString() method takes a parameter of the type char and returns the corresponding String object that represents the specified character.

Checking For Upper Case and Lower Case

The following program also shows that before converting to uppercase or lowercase, first check the input string. For this purpose, we use the following two methods.

The isLowerCase() Method of the Character Class

Signature: public static boolean toLowerCase(char my_char)

This method takes a parameter of the type char and returns a boolean value of true or false. In other words, when the given character is in lower case, it returns true. Otherwise, it returns false.

Similarly, we can use the isUpperCase() method.

The isUpperCase() Method of the Character Class

Signature: public static boolean toLowerCase(char my_char)

This method takes a parameter of type character and returns a boolean value of true if the parameter is in lowercase. Otherwise, it returns false.

Converting to Upper Case or Lower Case

For the purpose of converting a string to upper case or lower case, we use toUpperCase() and toLowerCase() methods of the String class.

The toUpperCase() Method

Signature: public String toUpperCase()

Basically, this method converts the String object through which it is called to upper case. Furthermore, it returns a new string and doesn’t change the original string.

The toLowerCase() Method

Signature: public String toLowerCase()

Similarly, this method converts the given string to lower case and returns a new string.

public class Exercise7
{
	public static void main(String[] args) {
		char input;
		String output="";
		//input='g';
		input='F';
		String s1=Character.toString(input);
		if(Character.isLowerCase(input))
		{
			output=s1.toUpperCase();
		}
		if(Character.isUpperCase(input))
		{
			output=s1.toLowerCase();
		}
		System.out.println(input+"->"+output);
	}
}

Output

An Example of Uppercase and Lowercase Functions in Java
An Example of Uppercase and Lowercase Functions in Java


Further Reading

Java Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *