Check Case of a Character Example in Java
On this page (10sections)
Introduction
Check Case Of A Character is a classic Java console program that demonstrates the concept with complete source code and sample output. Strings are immutable objects in Java; the examples show comparison, searching and transformation.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. A string is generally understood as a data type and is often implemented as an array of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. The case of a particular string can be found using inbuilt methods. These methods checks the case of each character in the string.
Syntax
String_Variable_Name.isUppercase()
Or
String_Variable_Name.isLowercase()
Check Case Of A Character Example Program
public class CheckCaseOfCharacter {
public static void main(String[] args) {
String characters = "AbCdefGHijklMNOpqRstUVwxyZ@%*765";
int length = characters.length();
for(int i = 0; i < length; i++) {
char character = characters.charAt(i);
if(Character.isUpperCase(character)) {
System.out.println("Given character : " + characters.charAt(i)+ " is an Uppercase letter");
}
else if(Character.isLowerCase(character)) {
System.out.println("Given character : " + characters.charAt(i)+ " is a Lowercase letter");
}
else {
System.out.println("Given character : " + characters.charAt(i)+ " is neither Uppercase nor Lowercase letter");
}
}
}
}
Sample Output
Given character : A is an Uppercase letter
Given character : b is a Lowercase letter
Given character : C is an Uppercase letter
Given character : d is a Lowercase letter
Given character : e is a Lowercase letter
Given character : f is a Lowercase letter
Given character : G is an Uppercase letter
Given character : H is an Uppercase letter
Given character : i is a Lowercase letter
Given character : j is a Lowercase letter
Given character : k is a Lowercase letter
Given character : l is a Lowercase letter
Given character : M is an Uppercase letter
Given character : N is an Uppercase letter
Given character : O is an Uppercase letter
Given character : p is a Lowercase letter
Given character : q is a Lowercase letter
Given character : R is an Uppercase letter
Given character : s is a Lowercase letter
Given character : t is a Lowercase letter
Given character : U is an Uppercase letter
Given character : V is an Uppercase letter
Given character : w is a Lowercase letter
Given character : x is a Lowercase letter
Given character : y is a Lowercase letter
Given character : Z is an Uppercase letter
Given character : @ is neither Uppercase nor Lowercase letter
Given character : % is neither Uppercase nor Lowercase letter
Given character : * is neither Uppercase nor Lowercase letter
Given character : 7 is neither Uppercase nor Lowercase letter
Given character : 6 is neither Uppercase nor Lowercase letter
Given character : 5 is neither Uppercase nor Lowercase letter
When to use
Use string manipulation when cleaning user input, parsing text files or formatting messages.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
String characters = "AbCdefGHijklMNOpqRstUVwxyZ@%*765";updates a variable used in the calculation or output. -
int length = characters.length();updates a variable used in the calculation or output. -
A loop repeats the block until its condition becomes false.
-
char character = characters.charAt(i);updates a variable used in the calculation or output. -
The
ifstatement runs the nested code only when the condition is true. -
A
println/printcall writes text to the console — part of the sample output below. -
The
ifstatement runs the nested code only when the condition is true.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.