🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Character.isUpperCase() method in Java is used to determine if a specified character is an uppercase letter.
Table of Contents
- Introduction
isUpperCase()Method Syntax- Examples
- Checking Single Characters
- Handling Non-Uppercase Characters
- Checking Multiple Characters
- Real-World Use Case
- Conclusion
Introduction
The Character.isUpperCase() method is a static method in the Character class in Java. It checks whether a given character is an uppercase letter. This method is useful when you need to validate or process text to ensure it conforms to certain casing rules.
isUpperCase()() Method Syntax
The Character.isUpperCase() method has two overloaded versions:
isUpperCase(char ch)
public static boolean isUpperCase(char ch)
- ch: The character to be tested.
The method returns:
trueif the character is an uppercase letter.falseotherwise.
isUpperCase(int codePoint)
public static boolean isUpperCase(int codePoint)
- codePoint: The Unicode code point to be tested.
The method returns:
trueif the code point is an uppercase letter.falseotherwise.
Examples
Checking Single Characters
The isUpperCase(char ch) method can be used to check if a single character is an uppercase letter.
Example
public class IsUpperCaseExample {
public static void main(String[] args) {
char upperCaseChar = 'A';
char lowerCaseChar = 'a';
boolean isUpperCase1 = Character.isUpperCase(upperCaseChar);
boolean isUpperCase2 = Character.isUpperCase(lowerCaseChar);
System.out.println("Is 'A' an uppercase letter? " + isUpperCase1);
System.out.println("Is 'a' an uppercase letter? " + isUpperCase2);
}
}
Output:
Is 'A' an uppercase letter? true
Is 'a' an uppercase letter? false
In this example, the method returns true for the uppercase letter 'A' and false for the lowercase letter 'a'.
Checking Unicode Code Points
The isUpperCase(int codePoint) method can be used to check if a Unicode code point is an uppercase letter.
Example
public class IsUpperCaseCodePointExample {
public static void main(String[] args) {
int upperCodePoint = 'B';
int lowerCodePoint = 'b';
boolean isUpperCase1 = Character.isUpperCase(upperCodePoint);
boolean isUpperCase2 = Character.isUpperCase(lowerCodePoint);
System.out.println("Is code point 'B' an uppercase letter? " + isUpperCase1);
System.out.println("Is code point 'b' an uppercase letter? " + isUpperCase2);
}
}
Output:
Is code point 'B' an uppercase letter? true
Is code point 'b' an uppercase letter? false
Handling Non-Uppercase Characters
The isUpperCase() method returns false for characters that are not uppercase letters.
Example
public class NonUpperCaseCharacterExample {
public static void main(String[] args) {
char digitChar = '1';
char specialChar = '$';
boolean isUpperCase1 = Character.isUpperCase(digitChar);
boolean isUpperCase2 = Character.isUpperCase(specialChar);
System.out.println("Is '1' an uppercase letter? " + isUpperCase1);
System.out.println("Is '$' an uppercase letter? " + isUpperCase2);
}
}
Output:
Is '1' an uppercase letter? false
Is '$' an uppercase letter? false
Checking Multiple Characters
You can check multiple characters to determine which ones are uppercase letters.
Example
public class MultipleCharactersExample {
public static void main(String[] args) {
char[] chars = { 'A', 'b', 'C', 'd', 'E', '1', '$' };
for (char ch : chars) {
boolean isUpperCase = Character.isUpperCase(ch);
System.out.printf("Is '%c' an uppercase letter? %b%n", ch, isUpperCase);
}
}
}
Output:
Is 'A' an uppercase letter? true
Is 'b' an uppercase letter? false
Is 'C' an uppercase letter? true
Is 'd' an uppercase letter? false
Is 'E' an uppercase letter? true
Is '1' an uppercase letter? false
Is '$' an uppercase letter? false
Real-World Use Case
Converting Text to Uppercase
In a real-world application, you might need to ensure that a string contains only uppercase letters before processing it further.
Example
public class ConvertToUpperCaseExample {
public static void main(String[] args) {
String text = "Hello World!";
StringBuilder upperText = new StringBuilder();
for (char ch : text.toCharArray()) {
if (Character.isUpperCase(ch)) {
upperText.append(ch);
} else {
upperText.append(Character.toUpperCase(ch));
}
}
System.out.println("Converted text: " + upperText.toString());
}
}
Output:
Converted text: HELLO WORLD!
In this example, the code converts all characters in the input string to uppercase.
Conclusion
The Character.isUpperCase() method in Java is a simple and effective way to check if a character is an uppercase letter. By understanding how to use this method and its overloaded versions, you can efficiently handle text processing tasks that involve checking for uppercase letters in your Java applications. Whether you are validating individual characters, handling Unicode code points, or processing entire strings, the isUpperCase() method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment