Primitive char vs. Character Class
In Java, the char is a primitive type that represents a single 16-bit Unicode character. Java also provides the Character wrapper class that wraps a value of the primitive type char in an object.
The Character class provides several useful methods for manipulating characters, such as determining if a character is a digit, letter, whitespace, etc. It's particularly useful when you need to treat a character as an object, for example, to store it in a collection (which can only hold objects).
The Character class is part of Java's java.lang package, so you don't need to import it explicitly.
Special Characters in Java
Escape sequences are special characters that have a particular meaning in Java. They are used to represent characters that cannot be typed directly, such as newline, tab, or quotation marks.
Escape sequences start with a backslash (\) followed by a character. Here are the most common escape sequences in Java:
\t- Inserts a tab in the text\b- Inserts a backspace\n- Inserts a new line\r- Inserts a carriage return\f- Inserts a form feed\'- Inserts a single quote\"- Inserts a double quote\\- Inserts a backslash
public class EscapeSequencesExample {
public static void main(String[] args) {
System.out.println("Hello\tWorld"); // Tab
System.out.println("Hello\nWorld"); // New line
System.out.println("She said: \"Hello\""); // Double quote
System.out.println("It's a backslash: \\"); // Backslash
}
}
Common Methods of the Character Class
The Character class provides many useful methods for working with characters. These methods are all static, which means you can call them directly on the class without creating an object.
Let's explore some of the most important methods in the following sections.
Checking if a Character is a Letter
The isLetter() method determines whether the specified character is a letter. It returns true if the character is a letter; false otherwise.
public class IsLetterExample {
public static void main(String[] args) {
char ch1 = 'A';
char ch2 = '1';
char ch3 = '$';
System.out.println(Character.isLetter(ch1)); // true
System.out.println(Character.isLetter(ch2)); // false
System.out.println(Character.isLetter(ch3)); // false
}
}
Checking if a Character is a Digit
The isDigit() method determines whether the specified character is a digit. It returns true if the character is a digit; false otherwise.
public class IsDigitExample {
public static void main(String[] args) {
char ch1 = '5';
char ch2 = 'A';
char ch3 = '@';
System.out.println(Character.isDigit(ch1)); // true
System.out.println(Character.isDigit(ch2)); // false
System.out.println(Character.isDigit(ch3)); // false
}
}
Checking if a Character is Whitespace
The isWhitespace() method determines whether the specified character is whitespace. It returns true if the character is whitespace; false otherwise. Whitespace includes space, tab, newline, carriage return, and form feed.
public class IsWhitespaceExample {
public static void main(String[] args) {
char ch1 = ' ';
char ch2 = '\t';
char ch3 = '\n';
char ch4 = 'A';
System.out.println(Character.isWhitespace(ch1)); // true
System.out.println(Character.isWhitespace(ch2)); // true
System.out.println(Character.isWhitespace(ch3)); // true
System.out.println(Character.isWhitespace(ch4)); // false
}
}
Checking if a Character is Uppercase
The isUpperCase() method determines whether the specified character is an uppercase letter. It returns true if the character is uppercase; false otherwise.
public class IsUpperCaseExample {
public static void main(String[] args) {
char ch1 = 'A';
char ch2 = 'a';
char ch3 = '1';
System.out.println(Character.isUpperCase(ch1)); // true
System.out.println(Character.isUpperCase(ch2)); // false
System.out.println(Character.isUpperCase(ch3)); // false
}
}
Checking if a Character is Lowercase
The isLowerCase() method determines whether the specified character is a lowercase letter. It returns true if the character is lowercase; false otherwise.
public class IsLowerCaseExample {
public static void main(String[] args) {
char ch1 = 'a';
char ch2 = 'A';
char ch3 = '1';
System.out.println(Character.isLowerCase(ch1)); // true
System.out.println(Character.isLowerCase(ch2)); // false
System.out.println(Character.isLowerCase(ch3)); // false
}
}
Converting a Character to Uppercase
The toUpperCase() method converts the specified character to uppercase. If the character is already uppercase or not a letter, it returns the character unchanged.
public class ToUpperCaseExample {
public static void main(String[] args) {
char ch1 = 'a';
char ch2 = 'A';
char ch3 = '1';
System.out.println(Character.toUpperCase(ch1)); // A
System.out.println(Character.toUpperCase(ch2)); // A
System.out.println(Character.toUpperCase(ch3)); // 1
}
}
Converting a Character to Lowercase
The toLowerCase() method converts the specified character to lowercase. If the character is already lowercase or not a letter, it returns the character unchanged.
public class ToLowerCaseExample {
public static void main(String[] args) {
char ch1 = 'A';
char ch2 = 'a';
char ch3 = '1';
System.out.println(Character.toLowerCase(ch1)); // a
System.out.println(Character.toLowerCase(ch2)); // a
System.out.println(Character.toLowerCase(ch3)); // 1
}
}
Converting a Character to a String
The toString() method returns a String object representing the specified character value. There are two versions of this method:
static String toString(char c)- Returns aStringobject representing the specified character.String toString()- Returns aStringobject representing the value of thisCharacterobject.
public class ToStringExample {
public static void main(String[] args) {
char ch = 'A';
// Static method
String str1 = Character.toString(ch);
System.out.println(str1); // A
// Using Character object
Character charObj = new Character(ch);
String str2 = charObj.toString();
System.out.println(str2); // A
}
}
Test Your Skills
- Write a program to count the number of letters, digits, and whitespace characters in a given string.
- Write a program to convert all lowercase letters in a string to uppercase and vice versa.
- Write a program to check if a string is a palindrome (ignoring case and non-letter characters).
- Write a program to count the number of words in a string (a word is a sequence of characters separated by whitespace).
- Write a program to remove all digits from a string.
🏆 Challenge: Character Analyzer
Create a program that analyzes a string and provides detailed statistics about the characters it contains, including the count of letters (uppercase and lowercase), digits, whitespace, and other characters. The program should also convert the string to all uppercase and all lowercase.
import java.util.Scanner;
public class CharacterAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- Character Analyzer ---");
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int letters = 0, digits = 0, whitespace = 0, uppercase = 0, lowercase = 0, others = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
letters++;
if (Character.isUpperCase(ch)) {
uppercase++;
} else {
lowercase++;
}
} else if (Character.isDigit(ch)) {
digits++;
} else if (Character.isWhitespace(ch)) {
whitespace++;
} else {
others++;
}
}
System.out.println("\n--- Statistics ---");
System.out.println("Total characters: " + input.length());
System.out.println("Letters: " + letters + " (Uppercase: " + uppercase + ", Lowercase: " + lowercase + ")");
System.out.println("Digits: " + digits);
System.out.println("Whitespace: " + whitespace);
System.out.println("Others: " + others);
System.out.println("\n--- Conversions ---");
System.out.println("Uppercase: " + input.toUpperCase());
System.out.println("Lowercase: " + input.toLowerCase());
scanner.close();
}
}