ZetCode

Java Character Class

Last modified: April 13, 2025

The java.lang.Character class wraps a value of the primitive type char in an object. This class provides methods for determining a character's category (letter, digit, etc.) and for converting characters between different cases.

Character class is essential when working with individual characters in Java. It contains useful constants and static methods that can classify, transform, and perform operations on char values. The class handles Unicode characters and supports supplementary characters.

Character Class Methods

The Character class provides numerous static methods for character manipulation. These include classification methods, case conversion methods, and numeric value conversion methods. The class also contains useful constants like MIN_VALUE and MAX_VALUE.

public final class Character {
    public static final char MIN_VALUE = '\u0000';
    public static final char MAX_VALUE = '\uffff';
    
    public static boolean isLetter(char ch) {...}
    public static boolean isDigit(char ch) {...}
    public static boolean isWhitespace(char ch) {...}
    public static char toUpperCase(char ch) {...}
    public static char toLowerCase(char ch) {...}
    public static int digit(char ch, int radix) {...}
    public static char forDigit(int digit, int radix) {...}
    // ... many more methods
}

The code above shows some key methods and constants of the Character class. These methods operate on primitive char values and return various information about characters. The class is immutable and cannot be subclassed.

Character Classification

Character class provides several methods to determine a character's category. These include isLetter, isDigit, and isWhitespace. These methods are useful for input validation and text processing.

Main.java
package com.zetcode;

void main() {
    char ch1 = 'A';
    char ch2 = '4';
    char ch3 = '\t';
    
    System.out.println("Is 'A' a letter? " + Character.isLetter(ch1));
    System.out.println("Is 'A' a digit? " + Character.isDigit(ch1));
    System.out.println("Is '4' a digit? " + Character.isDigit(ch2));
    System.out.println("Is '\\t' whitespace? " + Character.isWhitespace(ch3));
    System.out.println("Is 'A' lowercase? " + Character.isLowerCase(ch1));
}

This example demonstrates character classification methods. We test different characters to see if they are letters, digits, or whitespace. The methods return boolean values indicating whether the character belongs to the specified category.

Case Conversion

The Character class provides methods for converting characters between upper and lower case. The toUpperCase and toLowerCase methods handle case conversion according to Unicode standards.

Main.java
package com.zetcode;

void main() {
    char lowerA = 'a';
    char upperA = 'A';
    char sigma = 'Σ'; // Greek capital letter sigma
    
    System.out.println("Uppercase of 'a': " + Character.toUpperCase(lowerA));
    System.out.println("Lowercase of 'A': " + Character.toLowerCase(upperA));
    System.out.println("Lowercase of 'Σ': " + Character.toLowerCase(sigma));
    
    // Check if characters are uppercase or lowercase
    System.out.println("Is 'A' uppercase? " + Character.isUpperCase(upperA));
    System.out.println("Is 'a' lowercase? " + Character.isLowerCase(lowerA));
}

This example shows case conversion methods. We convert characters between cases and check their case status. Note that these methods handle Unicode characters correctly, as demonstrated with the Greek capital letter sigma.

Digit Conversion

The Character class can convert between characters and their numeric values. The digit method returns the numeric value of a character in the specified radix, while forDigit does the reverse conversion.

Main.java
package com.zetcode;

void main() {
    char digitChar = '7';
    char hexChar = 'A';
    int digit = 12;
    
    // Convert character to numeric value
    System.out.println("Numeric value of '7': " + Character.digit(digitChar, 10));
    System.out.println("Hex value of 'A': " + Character.digit(hexChar, 16));
    
    // Convert numeric value to character
    System.out.println("Character for 12 in radix 16: " + 
                      Character.forDigit(digit, 16));
    System.out.println("Character for 9 in radix 10: " + 
                      Character.forDigit(9, 10));
}

This example demonstrates digit conversion methods. We convert characters to their numeric values in different bases (radix) and vice versa. These methods are particularly useful when working with different number systems.

Character Comparison

The Character class provides methods for comparing characters. The compare method compares two char values numerically, while compareTo compares two Character objects.

Main.java
package com.zetcode;

void main() {
    char ch1 = 'A';
    char ch2 = 'B';
    Character charObj1 = 'A';
    Character charObj2 = 'B';
    
    // Compare primitive chars
    System.out.println("Compare 'A' and 'B': " + Character.compare(ch1, ch2));
    
    // Compare Character objects
    System.out.println("Compare Character 'A' and 'B': " + 
                      charObj1.compareTo(charObj2));
    
    // Equality checks
    System.out.println("'A' equals 'A'? " + charObj1.equals('A'));
    System.out.println("'A' equals 'B'? " + charObj1.equals('B'));
}

This example shows different ways to compare characters. The compare method returns a negative, zero, or positive integer depending on the comparison. The equals method checks for exact equality between Character objects and primitive values.

Unicode Code Points

The Character class supports Unicode code points, which can represent characters outside the BMP (Basic Multilingual Plane). Methods like isLetter and toUpperCase have code point variants for supplementary characters.

Main.java
package com.zetcode;

void main() {
    // A supplementary character (outside BMP)
    int codePoint = 0x1F600; // 😀 emoji
    
    System.out.println("Is 😀 a letter? " + 
                      Character.isLetter(codePoint));
    System.out.println("Is 😀 a valid code point? " + 
                      Character.isValidCodePoint(codePoint));
    System.out.println("Code point char count: " + 
                      Character.charCount(codePoint));
    
    // Convert to chars
    char[] chars = Character.toChars(codePoint);
    System.out.println("Chars for code point: " + 
                      new String(chars));
}

This example demonstrates working with Unicode code points. We check properties of a supplementary character (the smiling face emoji) that requires two char values (a surrogate pair) to represent. The charCount method returns 2 for such characters.

Character Constants

The Character class defines several useful constants. These include MIN_VALUE and MAX_VALUE representing the range of char values, and constants for specific Unicode values like MIN_RADIX and MAX_RADIX.

Main.java
package com.zetcode;

void main() {
    System.out.println("Minimum char value: " + (int) Character.MIN_VALUE);
    System.out.println("Maximum char value: " + (int) Character.MAX_VALUE);
    System.out.println("Minimum radix: " + Character.MIN_RADIX);
    System.out.println("Maximum radix: " + Character.MAX_RADIX);
    
    // Unicode constants
    System.out.println("Unicode space separator: " + 
                      Character.SPACE_SEPARATOR);
    System.out.println("Unicode lowercase letter: " + 
                      Character.LOWERCASE_LETTER);
}

This example shows some important constants defined in the Character class. These constants are useful when working with character ranges and Unicode categories. The MIN_VALUE and MAX_VALUE represent the range of possible char values in Java.

Character Escapes

The Character class can help identify escape sequences in strings. While Java handles most escapes at compile time, Character methods can detect special characters that might need escaping in certain contexts.

Main.java
package com.zetcode;

void main() {
    char newline = '\n';
    char tab = '\t';
    char backslash = '\\';
    
    System.out.println("Is '\\n' whitespace? " + 
                      Character.isWhitespace(newline));
    System.out.println("Is '\\t' whitespace? " + 
                      Character.isWhitespace(tab));
    System.out.println("Is '\\\\' a letter? " + 
                      Character.isLetter(backslash));
    
    // Escape sequences in strings
    System.out.println("Line 1" + newline + "Line 2");
    System.out.println("Column1" + tab + "Column2");
}

This example demonstrates how Character methods can identify special characters like newlines and tabs. While these are escape sequences in Java source code, they become single char values at runtime. The methods can detect their special properties.

Source

Java Character Class Documentation

In this article, we've covered the essential methods of the Java Character class with practical examples. Understanding these methods is crucial for proper text processing and character manipulation in Java applications.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.