Character Wrapper Class and Its Methods in Java



In this article, we will learn about the character wrapper class and its methods in Java. The Character class is the primitive char type wrapper, providing useful methods for manipulation, classification, and conversion of characters.

The Character Wrapper Class

The Character class of the java.lang package wraps a value of the primitive datatype char. It offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor.

Syntax

Character ch = new Character('a');

Various Methods in the Character Class

The following are some of the methods used in the Character class ?

1

isLetter()

Determines whether the specified char value is a letter.

2

isDigit()

Determines whether the specified char value is a digit.

3

isWhitespace()

Determines whether the specified char value is white space.

4

isUpperCase()

Determines whether the specified char value is uppercase.

5

isLowerCase()

Determines whether the specified char value is lowercase.

6

toUpperCase()

Returns the uppercase form of the specified char value.

7

toLowerCase()

Returns the lowercase form of the specified char value.

8

toString()

Returns a String object representing the specified character value that is, a one-character string.

Using the isDigit() Method

The following are the steps to check whether the character is a digit or not using the isDigit() method ?

  • Declaring and Initializing Characters: ch1 = '9' and ch2 = 'V' represent two different character values.
  • Checking if Characters Are Digits:
    • Character.isDigit(ch1) returns true because '9' is a numeric digit.
    • Character.isDigit(ch2) returns false because 'V' is not a digit.
b1 = Character.isDigit(ch1);
b2 = Character.isDigit(ch2);

Example

Below is an example to check whether the character is a digit or not ?

public class CharacterClassExample {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '9';
      ch2 = 'V';
      
      boolean b1, b2;
      b1 = Character.isDigit(ch1);
      b2 = Character.isDigit(ch2);
      
      String str1 = ch1 + " is a digit is " + b1;
      String str2 = ch2 + " is a digit is " + b2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

9 is a digit is true
V is a digit is false

Time complexity: O(1) as Character.isDigit() runs in constant time.
Space complexity: O(1) since only a few variables are used.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-26T12:06:18+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements