Java Character With Examples

In order to capture and process the textual information in Java, we use String. If we look closely, String is nothing but a collection/group of characters. So we can say, characters are the basic building unit of string.

Let us discuss Character in this lecture.

As already discussed in Java data types, we have a primitive data type char that can be used when working with characters.

The character can be declared in the following ways:

Default declaration

char myChar1; // This assigns default value to myChar1

Declaration with value

char myChar2 = 'g';     //assigning lowercase g to myChar2
char myChar3 = '8';     //assigning 8 as character to myChar3
char myChar4 = (char) 76; //assigning type casted integer to myChar4
char myChar5 = '\n';    //assigning escape sequence in single quotes to myChar5

We have seen default declaration and declaration with value(the first two examples). However, there are two new things which we will discuss here.

  1. Number code typecasted
  2. Escape sequence

1. Number code typecasted

Java uses number code to represent a character. Java earlier used ASCII (American Standard Code for Information Interchange). It is a collection of 127 unique characters which include numbers, alphabets(both lowercase and uppercase), special character, escape sequence(we will discuss it). But in order to extend this set to 65536 unique characters comprising alphabets of almost every language, Java started using Unicode Character Set.  

Let us look into some commonly used character codes, that are used frequently.

Characters ASCII Code
A to Z 65 to 90
a to z 97 to 122
0 to 9 48 to 57

So it means

char myChar4 = (char) 76 represents, that myChar4 = 'L'.

If we do the vice versa

int myInt = (int)'L' represents that myInt stores ASCII value of L i.e myInt = 76 .

2. Escape sequence

Java has predefined escape sequences and holds a special meaning to its compiler. These are represented by a character with a backslash (\). It is used to store non-printable characters or print characters with a pre-defined value in Java.  

Example of non-printable are:

Escape Sequence Description
\n Inserts newline
\b Inserts backspace
\t Inserts tab
\r Inserts carriage return
\f Inserts form-feed

Example of Character with pre-defined special meaning in Java:

Escape Sequence Description
\’ Inserts single quote
\” Inserts double quote
\\ Inserts backslash

Arithmetic Operations on Characters

As mentioned earlier in this lecture, the characters are represented as a number code. Hence arithmetic operations are also possible on characters.

Example:

public class MyChar{
    public static void main(String args[])
    {
        char myNormalChar = 'h';
        char myNormalChar2 = 'M';
        System.out.println("\nBefore: myNormalChar = "+ myNormalChar);
        System.out.println("Before: myNormalChar2 = "+ myNormalChar2);
        myNormalChar++; //This increments the ASCII number code by one.Hence, h turns to i
        
        myNormalChar2--;//This decreases the ASCII number code by one. Hence, M turns to L
        
        System.out.println("\nAfter: myNormalChar = "+ myNormalChar);
        System.out.println("Before: myNormalChar2 = "+ myNormalChar2);
        
    }
}

Output:

Before: myNormalChar = h
Before: myNormalChar2 = M

After: myNormalChar = i
Before: myNormalChar2 = L

Character Class

Similar to class Number, we have a class Character, which is an alternative to primitive data type char. In our normal usage, we use primitive data type, but in order to make code strictly object-oriented, Character is used. The Character class is convertible to its primitive type char and vice versa. The concept of Autoboxing and Unboxing is applicable here as well. So there are two ways of creating a Character object:

1. Using Constructors: The Syntax to create a new Character object is

Character myCharObject = new Character(‘G’); //This statement uses new keyword which implicitly calls the constructor.

2. Autoboxing

Character myCharObject = ‘G’;

Character Methods

Following is the list of the important instance methods that all the subclasses of the Character class implement −

There are several methods of Character class each dedicated to its purpose. Let us explore a few of them.

Java.lang.Character.isDigit() Method:

Name: isDigit
Signature: Character.isDigit(characterToTest);
Output:boolean
Description: It determines if the Character is a digit or not

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.isDigit('G'));   //This is digit
        System.out.println(Character.isDigit('2'));   //This is not a digit
    }
}

Output:

false
true

Java.lang.Character.isLetter() Method:

 Name: isLetter()
Signature: Character.isLetter(characterToTest);
Output:boolean
Description: It determines if the Character is a letter or not

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.isLetter('G'));   //This is letter 
        System.out.println(Character.isLetter('2'));   //This is not a letter
    }
}

Output:

true
false

Java.lang.Character.isLowerCase() Method:

 Name: isLowerCase()
Signature: Character.isLowerCase(characterToTest);
Output:boolean
Description: It determines if the Character is a lower-case letter or not

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.isLowerCase('G'));   //This is upper-case letter 
        System.out.println(Character.isLowerCase('g'));   //This is lower-case letter
    }
}

Output:

false
true

Java.lang.Character.isUpperCase() Method:

 Name: isUpperCase()
Signature: Character.isUpperCase(characterToTest);
Output:boolean
Description: It determines if the Character is a upper-case letter or not

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.isUpperCase('G'));   //This is upper-case letter 
        System.out.println(Character.isLowerCase('g'));   //This is lower-case letter
    }
}

Output:

true
false

Java.lang.Character.toLowerCase() Method:

 Name: toLowerCase()
Signature: Character.toLowerCase(characterToTest);
Output: Character
Description: Converts to character to lower-case

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.toLowerCase('M'));
        System.out.println(Character.toLowerCase('G'));
    }
}

Output:

m
g

Java.lang.Character.toUpperCase() Method:

 Name: toUpperCase()
Signature: Character.toUpperCase(characterToTest);
Output: Character
Description: Converts to character to upper-case

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.toUpperCase('m'));
        System.out.println(Character.toUpperCase('g'));
    }
}

Output:

M
G

Java.lang.Character.toString() Method:

 Name: toString()
Signature: Character.toString(characterToTest);
Output: String
Description: Converts to Character to String object

Example:

public class MyChar{
    public static void main(String args[])
    {
        System.out.println(Character.toString('M'));
    }
}

Output:

M

This was just a glimpse of methods and features that are provided by Character class. We will discuss Java String in the next class.