✏️ Explanatory Question
| 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. |
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() ExampleWith 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
isUpperCase() to check if a character is uppercase.toUpperCase() to convert characters or strings to uppercase.