Table of contents
1.
Introduction
2.
Constructor and Field
3.
Methods of Character Class
4.
Examples
4.1.
Java
4.2.
Java
4.3.
Java
4.4.
Java
4.5.
Java
4.6.
Java
4.7.
Java
5.
How to Add Character Object in Java?
5.1.
1. Using the Character Wrapper Class
5.2.
2. Autoboxing
5.3.
3. Storing in a Collection
5.4.
4. Using Character Utility Methods
6.
Frequently Asked Questions
6.1.
What is a Java Character Class?
6.2.
What is an Example of a Character Class?
6.3.
Which Package Has the Character Class in Java?
7.
Conclusion
Last Updated: Nov 25, 2024
Easy

Character Class in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We will be looking into the Character Class of Java. It is also called wrapper class in Java, and this name is given to it because of its work of wrapping the primitive data type (generally char type) to its object. Consider this article to learn more about Data types in Java. Character Class also provides us with a constructor to create an object.

Character Class in Java

In this article, we will get ourselves acquainted with several static methods of the Character class of Java. If you are not familiar with the static keyword, check out our blog on Static Keywords in Java first.

Constructor and Field

To use any of the features of this class. First, you need to import its package java.lang package. In Java's Character class, there is only one field of type char present in the class, and also, there is only one constructor to initialize that field. How to use the constructor to initialize the field is shown below.

Character cha = Character(‘C’); 


Here, character ‘C’ is assigned to the object ‘cha’.

Also see,  Swap Function in Java

Methods of Character Class

There are several methods of character class. Some of the important methods are described below.

S.No.MethodsDescription
1charCount(int codePoint)It returns the number of char values that are required to represent the given character.
2charValue()It returns the value of the given Character’s object.
3compare(char x, char y)It compares numerically two-character type objects.
4compareTo(Character anotherCharacter)It compares numerically two Character type objects.
5digit(char ch, int radix)It returns the numeric value for the given character in the specified index.
6equals(object obj)It compares the given with the specified object.
7getNumericValue(char ch)It returns the integer value of the specified Unicode character.
8getType(char ch)It returns the value which indicates the general category of the character.
9hashcode()It returns the hash code for the specified character. It works the same as invoking the charValue() method.
10hashcode(char value)It returns the hash code for a particular char value.
11isDefined(char ch)It determines whether the character is defined in the Unicode or not.
12isDefined(int codePoint)It determines whether the character(Unicode code point) is defined in the Unicode or not.
13isLetter(char ch)It determines whether the specified character is a letter or not.
14isLetter(int codePoint)It determines whether the specified character(Unicode code point) is a letter or not.
15isLowerCase(char ch)It determines whether the specified character is a lowercase character or not.
16isUpperCase(char ch)It determines whether the given character is an uppercase character or not.
17isWhitespace(char ch)It determines whether the given character is a white space or not.
18toLowerCase(char ch)This method converts the character type argument to lowercase using case mapping information obtained from the Unicode Data file.
19toUpperCase(char ch)This method converts the character type argument to uppercase using case mapping information obtained from the Unicode Data file.
20valueOf(char c)It returns the instance of a Character that represents the specified character value.

 

Also read, Duck Number in Java and Hashcode Method in Java

Examples

Now, we will discuss some of the methods from the above table with examples.

1. boolean isUpperCase(char ch):

Syntax:

 boolean isUpperCase(char ch)

Code: 

  • Java

Java

public class Demo {
//In this function we will check whether the character is uppercase or not
   public static void main(String[] args)
   {
       System.out.println("Is C is upper case: "+Character.isUpperCase('C'));
       System.out.println("Is n is upper case: "+Character.isUpperCase('n'));
       System.out.println("Is 71(ASCII value) is upper case: "+Character.isUpperCase(71));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Is C is upper case: true
Is n is upper case: false
Is 71(ASCII value) is upper case: true

 

2. boolean isLowerCase(char ch)

Syntax: 

boolean isLowercase(char ch)

Code:

  • Java

Java

public class Demo {
//In this function, we will check whether the character is lowercase or not
   public static void main(String[] args)
   {
       System.out.println("Is C is lower case: "+Character.isLowerCase('C'));
       System.out.println("Is n is lower case: "+Character.isLowerCase('n'));
       System.out.println("Is 78(ASCII value) is lower case: "+Character.isLowerCase(78));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Is C is lower case: false
Is n is lower case: true
Is 78(ASCII value) is lower case: false


Try it on java online compiler.


3. char toUpperCase(char ch)

Syntax: 

char toUpperCase(char ch)

Code:

  • Java

Java

public class Demo {
   public static void main(String[] args)
   {
       //This function will cast the character to the uppercase character
       System.out.println("Upper case of c is: "+Character.toUpperCase('c'));
       //Using ASCII value of alphabet.
       System.out.println("Upper case of 101 is: "+Character.toUpperCase(101));
       System.out.println("Upper case of 50 is: "+Character.toUpperCase(50));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Upper case of c is: C
Upper case of 101 is: 69
Upper case of 50 is: 50

 

4. char toLowerCase(char ch)

Syntax: 

char toLowerCase(char ch)

Code:

  • Java

Java

public class Demo {
   public static void main(String[] args)
   {
//This function will cast the character to the lowercase character
       System.out.println("Lower case of n: "+Character.toLowerCase('N'));
       System.out.println("Lower case of 101: "+Character.toLowerCase(101));
       System.out.println("Lower case of 69: "+Character.toLowerCase(69));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Lower case of n: n
Lower case of 101: 101
Lower case of 69: 101

 

5. toString(char ch)

Syntax: 

String toString(char ch)

Code:

  • Java

Java

public class Demo {
   public static void main(String[] args)
   {
//This function will change the char to string
       System.out.println("Changing char C to string C: "+Character.toString('C'));
       System.out.println("Changing char C to string C: "+Character.toString('N'));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Changing char C to string C: C
Changing char C to string C: N

 

6. boolean isWhitespace(char ch)

Syntax: 

boolean isWhitespace(char ch)

Code:

  • Java

Java

public class Demo {
   public static void main(String[] args)
   {
//This function will tell whether there is a whitespace or not.
       System.out.println("Back slash t have whitespace: "+Character.isWhitespace('\t')); 
       System.out.println("9 have whitespace: "+Character.isWhitespace('9'));
       //ASCII value of Tab is 9.
       System.out.println("' ' have whitespace: "+Character.isWhitespace(' '));
       System.out.println("Back slash n have whitespace: "+Character.isWhitespace('\n'));
       System.out.println("C have whitespace: "+Character.isWhitespace('C'));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Backslash t have whitespace: true
9 have whitespace: false
' ' have whitespace: true
Backslash n have whitespace: true
C have whitespace: false

 

7. hashCode(char ch)

Syntax: 

int hashCode(char ch)

Code:

  • Java

Java

public class Demo {
   public static void main(String[] args)
   {
//It will return the hash code of the character
       System.out.println("Hash code of back slash t: "+Character.hashCode('\t')); 
       System.out.println("Hash code of C: "+Character.hashCode('C'));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Hash code of backslash t: 9
Hash code of C: 67

How to Add Character Object in Java?

1. Using the Character Wrapper Class

The Character class wraps a value of the primitive type char in an object. Here's an example:

// Creating a Character object
Character charObj = Character.valueOf('A');

// Printing the Character object
System.out.println("Character Object: " + charObj);

// Converting back to primitive char
char primitiveChar = charObj.charValue();
System.out.println("Primitive char: " + primitiveChar);

Explanation:

  • valueOf(char c): This static method of the Character class creates and returns a Character object for the given char.
  • charValue(): This method retrieves the char value from the Character object.

2. Autoboxing

Java automatically converts a char into a Character object when needed, thanks to autoboxing.

// Autoboxing a char to Character
Character charObj = 'B';
System.out.println("Autoboxed Character Object: " + charObj);

Explanation:

  • Autoboxing simplifies code by implicitly wrapping char into a Character

3. Storing in a Collection

You can add Character objects to a collection, such as a list:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // List to store Character objects
        List<Character> charList = new ArrayList<>();

        // Adding Character objects
        charList.add('C');
        charList.add('D');

        // Printing the list
        System.out.println("Character List: " + charList);
    }
}

Explanation:

  • You cannot store primitive char values directly in collections because collections store objects.
  • Java automatically converts char to Character when adding to a collection (autoboxing).

4. Using Character Utility Methods

The Character class also provides utility methods for character operations:

Character charObj = 'E';

// Checking if it is a letter
System.out.println("Is Letter: " + Character.isLetter(charObj));

// Converting to uppercase
System.out.println("Uppercase: " + Character.toUpperCase(charObj));

Explanation:

  • isLetter(): Checks if the character is a letter.
  • toUpperCase(): Converts the character to uppercase.

Frequently Asked Questions

What is a Java Character Class?

The Character class in Java is a wrapper class for the char primitive, providing utility methods for character operations like case conversion and validation.

What is an Example of a Character Class?

An example is the Character class method Character.isLetter('A'), which checks if a character is a letter, returning true in this case.

Which Package Has the Character Class in Java?

The Character class is part of the java.lang package, which is automatically imported in all Java programs.

Conclusion

We have thoroughly discussed the  Character class in Java and its implementation. We discussed how its constructor, field, and methods could be used in Java.

We hope that this blog has helped you enhance your knowledge regarding the Character class in Java. We can use the concepts of Java in building an android app to learn in-depth about android development. Check out our Android Development course on the Code360 website. 

Live masterclass