how to convert char to uppercase java with code examples

Converting characters to uppercase is a common task in programming, and Java makes it easy to do using built-in String methods. Whether you need to convert a single character or an entire string, Java has methods that can help you easily achieve this. In this article, we will go over how to convert char to uppercase in Java with code examples.

Using the Character.toUpperCase() Method

The Character class in Java provides a toUpperCase() method that can be used to convert a single character to uppercase. The syntax for using this method is:

char ch = 'a';
char uppercaseCh = Character.toUpperCase(ch);

The toUpperCase() method takes a single character as an input and returns a new character that is the uppercase equivalent of the input character. In the above example, the variable uppercaseCh will contain the character 'A', which is the uppercase equivalent of the input character 'a'.

Here's an example program that demonstrates how to use the toUpperCase() method to convert a string to uppercase:

public class CharToUppercaseExample {
    public static void main(String[] args) {
        String str = "hello world";
        
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            char uppercaseCh = Character.toUpperCase(ch);
            System.out.print(uppercaseCh);
        }
    }
}

In this program, we first declare a string variable str that contains the text "hello world". We then loop through each character in the string using a for loop, and inside the loop, we use the toUpperCase() method to convert each character to uppercase.

The output of this program will be:

HELLO WORLD

Using the String.toUpperCase() Method

If you need to convert an entire string to uppercase, Java also provides a toUpperCase() method on the String class. The syntax for using this method is:

String str = "hello world";
String uppercaseStr = str.toUpperCase();

The toUpperCase() method takes no input parameters and returns a new string that is the uppercase equivalent of the input string.

Here's an example program that demonstrates how to use the toUpperCase() method to convert a string to uppercase:

public class StringToUppercaseExample {
    public static void main(String[] args) {
        String str = "hello world";
        String uppercaseStr = str.toUpperCase();
        System.out.println(uppercaseStr);
    }
}

In this program, we first declare a string variable str that contains the text "hello world". We then use the toUpperCase() method to convert the entire string to uppercase and store the result in a new string variable uppercaseStr. Finally, we print out the uppercase string to the console.

The output of this program will be:

HELLO WORLD

Conclusion

Converting characters to uppercase is a common task in programming, and Java makes it easy to do using built-in String methods. Whether you need to convert a single character or an entire string, Java has methods that can help you easily achieve this. In this article, we went over how to convert char to uppercase in Java with code examples. Hopefully, this will help you in your Java programming journey.

let's dive deeper into the two methods covered in the previous article – Character.toUpperCase() and String.toUpperCase() methods.

Character.toUpperCase() Method

The Character.toUpperCase() method is a member method of the Character class in Java. This method converts a given character to its uppercase equivalent based on the Unicode character set. If the provided character is already in uppercase, the method returns the same character.

Syntax:

public static char toUpperCase(char c)

Parameters:

  • c – the character to be converted to uppercase.

Return Value:

  • Returns the uppercase equivalent of the provided character.

Example:

char ch = 'a';
char upperCh = Character.toUpperCase(ch);

In the example above, the char variable ch is assigned 'a' and then the toUpperCase() method is called with ch as the argument. The resulting character 'A' is assigned to the variable upperCh.

String.toUpperCase() Method

The String.toUpperCase() method is a member method of the String class in Java. This method converts all the characters in a given string to their uppercase equivalent based on the Unicode character set.

Syntax:

public String toUpperCase()

Return Value:

  • Returns the uppercase equivalent of the provided string.

Example:

String str = "hello world";
String upperStr = str.toUpperCase();

In the example above, the String variable str is assigned "hello world" and then the toUpperCase() method is called on it. The resulting string "HELLO WORLD" is assigned to the variable upperStr.

It's important to note that both of these methods return a new character or string, respectively, rather than changing the original provided value. This means that if you want to change the original value, you will need to assign the result back to the original variable.

In conclusion, converting characters or strings to uppercase in Java is a simple task thanks to built-in methods like Character.toUpperCase() and String.toUpperCase(). These methods are easy to use and provide a quick and efficient way to change the case of your input data.

Popular questions

  1. What built-in Java class provides a method to convert a single character to uppercase?

Answer: The Character class in Java provides a toUpperCase() method that takes a single character as input and returns the uppercase equivalent of the character.

  1. How do you convert a single character to uppercase in Java using the Character class method?

Answer: To convert a single character to uppercase in Java using the Character class method, you can call the toUpperCase() method on the Character class, passing in the character you want to convert as a parameter. The method returns the uppercase equivalent of the character.

  1. How do you convert an entire string to uppercase in Java?

Answer: To convert an entire string to uppercase in Java, you can use the toUpperCase() method provided by the String class. This method takes no input parameters and returns a new string that is the uppercase equivalent of the input string.

  1. What happens if you call the toUpperCase() method on a string or character that is already in uppercase?

Answer: If you call the toUpperCase() method on a string or character that is already in uppercase, the method returns the same string or character without any changes.

  1. Can the toUpperCase() methods change the original input values?

Answer: No, both the Character.toUpperCase() and String.toUpperCase() methods return a new character or string, respectively, rather than changing the original provided value. This means that if you want to change the original value, you will need to assign the result back to the original variable.

Tag

"UppercaseConversion"

As an experienced software engineer, I have a strong background in the financial services industry. Throughout my career, I have honed my skills in a variety of areas, including public speaking, HTML, JavaScript, leadership, and React.js. My passion for software engineering stems from a desire to create innovative solutions that make a positive impact on the world. I hold a Bachelor of Technology in IT from Sri Ramakrishna Engineering College, which has provided me with a solid foundation in software engineering principles and practices. I am constantly seeking to expand my knowledge and stay up-to-date with the latest technologies in the field. In addition to my technical skills, I am a skilled public speaker and have a talent for presenting complex ideas in a clear and engaging manner. I believe that effective communication is essential to successful software engineering, and I strive to maintain open lines of communication with my team and clients.
Posts created 3227

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top