Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Constructor and Field
3.
Methods of Character Class
4.
Examples
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024

Character Class in Java

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In this blog, 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.

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of Character Class

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

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

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));
    }
}

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:

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));
    }
}

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:

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));
    }
}

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:

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));
    }
}

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:

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'));
    }
}

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:

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'));
    }
}

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:

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'));
     }
}

Output:

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

Frequently Asked Questions

  1. What is the meaning of wrapper class?
    A wrapper class is a type that wraps or contains primitive data types in its object. When we construct an object for a wrapper class, it includes a field where we can store primitive data types. In other words, a primitive value can be wrapped into a wrapper class object.
     
  2. What is the meaning of static methods?
    Its means these methods can be called without having an object. 
     
  3. Is ASCII value for tab and whitespace also exist?
    Yes, ASCII value of tab, whitespace, next line, escape, etc exists.

Conclusion

In this article, 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 Coding Ninjas website. Do upvote our blog to help other ninjas grow. 
Happy Coding!

Previous article
Wrapper class in Java
Next article
Interface in Java
Live masterclass