Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Character Class

The Character class in Java is a wrapper class for the primitive data type char. It provides utility methods for working with characters, such as determining if a character is a letter or digit, converting characters to upper or lower case, and more.

In this tutorial, we will go through examples of using the Character class.

  • Creating Character objects:

You can create Character objects using the constructor or the valueOf method:

char c = 'A';
Character char1 = new Character(c);
Character char2 = Character.valueOf(c);
  • Comparing Character objects:

To compare two Character objects, you can use the compareTo or equals method:

Character char1 = new Character('A');
Character char2 = new Character('B');

System.out.println(char1.compareTo(char2)); // Output: -1 (char1 < char2)
System.out.println(char1.equals(char2));    // Output: false
  • Converting Character objects to primitive chars:

To convert a Character object to a primitive char, you can use the charValue method:

Character charObj = new Character('A');
char charPrimitive = charObj.charValue();
  • Checking character properties:

The Character class provides several static methods to determine the properties of characters, such as checking if a character is a letter, digit, whitespace, etc.:

char c1 = 'A';
char c2 = '1';
char c3 = ' ';

System.out.println(Character.isLetter(c1));     // Output: true
System.out.println(Character.isDigit(c2));      // Output: true
System.out.println(Character.isWhitespace(c3)); // Output: true
  • Converting characters to upper or lower case:

You can use the Character class to convert characters to upper or lower case:

char c1 = 'a';
char c2 = 'B';

System.out.println(Character.toUpperCase(c1)); // Output: A
System.out.println(Character.toLowerCase(c2)); // Output: b
  • Unicode and code points:

The Character class provides methods to work with Unicode code points:

int codePoint = 65; // Unicode code point for the letter 'A'

// Convert a Unicode code point to a char
char charFromCodePoint = (char) codePoint;
System.out.println(charFromCodePoint); // Output: A

// Convert a char to a Unicode code point
int codePointFromChar = (int) charFromCodePoint;
System.out.println(codePointFromChar); // Output: 65

These are the basic usages of the Character class in Java. Using the Character class, you can perform various operations on characters and use them in object-oriented contexts.

  1. How to Check if a Character is a Digit in Java: Use the isDigit method to check if a character is a digit.

    char ch = '5';
    boolean isDigit = Character.isDigit(ch);
    
  2. Java Character Class isLetter Example: Determine if a character is a letter using the isLetter method.

    char ch = 'A';
    boolean isLetter = Character.isLetter(ch);
    
  3. Unicode and Java Character Class: Explore Unicode properties using the Character class.

    char ch = '\u0041'; // Unicode for 'A'
    int unicodeValue = Character.getNumericValue(ch);
    
  4. Convert char to int in Java using Character Class: Convert a character to its Unicode numeric value using getNumericValue method.

    char ch = '7';
    int numericValue = Character.getNumericValue(ch);
    
  5. Java Character Class isWhitespace Method: Check if a character is a whitespace character using isWhitespace.

    char ch = ' ';
    boolean isWhitespace = Character.isWhitespace(ch);
    
  6. Check if a Character is Uppercase in Java Character Class: Determine if a character is uppercase using isUpperCase method.

    char ch = 'X';
    boolean isUpperCase = Character.isUpperCase(ch);
    
  7. Java Character Class Constants and Values: Utilize constants and values provided by the Character class.

    char lowercaseA = Character.toLowerCase('A');
    char uppercaseX = Character.toUpperCase('x');
    
  8. Handling Special Characters in Java with Character Class: Use the Character class methods to handle special characters.

    char specialChar = '$';
    boolean isLetterOrDigit = Character.isLetterOrDigit(specialChar);