Write a difference between the functions isUpperCase( ) and toUpperCase( ).

Single Choice
Views 87

Answer:

isUpperCase( ) toUpperCase( )
isUpperCase( ) function checks if a given character is in uppercase or not. toUpperCase( ) function converts a given character to uppercase.
Its return type is boolean. Its return type is char.

Here’s the difference between the isUpperCase() and toUpperCase() methods in Java:


Aspect isUpperCase() toUpperCase()
Purpose Checks whether a character is uppercase. Converts a character or string to uppercase.
Return Type Returns a boolean (true or false). Returns a character or string in uppercase.
Input Takes a single char as input. Can take a single char or a String as input.
Usage Used for validation (e.g., checking letter case). Used for transformation (e.g., changing letter case).
Method in Part of the Character class. Part of the Character and String classes.

Examples

isUpperCase() Example


public class IsUpperCaseExample {
    public static void main(String[] args) {
        char ch = 'A';
        if (Character.isUpperCase(ch)) {
            System.out.println(ch + " is an uppercase letter.");
        } else {
            System.out.println(ch + " is not an uppercase letter.");
        }
    }
}

Output:


A is an uppercase letter.

toUpperCase() Example

With a Single Character:


public class ToUpperCaseExample {
    public static void main(String[] args) {
        char ch = 'a';
        char upper = Character.toUpperCase(ch);
        System.out.println("Uppercase: " + upper);
    }
}

Output:


Uppercase: A

With a String::


public class ToUpperCaseStringExample {
    public static void main(String[] args) {
        String str = "hello";
        String upperStr = str.toUpperCase();
        System.out.println("Uppercase String: " + upperStr);
    }
}

Output:


Uppercase String: HELLO

Key Takeaway

  • Use isUpperCase() to check if a character is uppercase.
  • Use toUpperCase() to convert characters or strings to uppercase.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.