Java Characters

In Java, char is the data type used to store characters. However, C/C++ programmers beware that the char in Java is not the same as char in C/C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent characters.

Java uses Unicode

Unicode defines a entirely international character set that can represent all of the characters found in all human languages. It's a unification of dozens of character sets, such as Latin, Arabic, Greek, Hebrew, Cyrillic, and many more.

At the time of Java's creation, Unicode required 16 bits. Thus, in Java char is a 16-bit type. The range of a char is from 0 to 65,536 range. There are no negative chars.

Standard set of Characters

The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the covered 8-bit character set, ISO-Latin-1, ranges from 0 to 255.

Since Java is designed to allow programs to be written for worldwide use, this makes the sense that it would use Unicode to represent the characters. Of course, the use of Unicode is slightly inefficient for languages such as English, Spanish, German, or French, whose characters can easily be controlled within 8 bits. But such is the price that must be paid for global portability.

Java Characters Example

Below is a program that demonstrates the char variables :

/* Java Program Example - Java Characters 
 * This program demonstrates char data type */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        char ch1, ch2;
        
        ch1 = 88;      // code for X
        ch2 = 'Y';
        
        System.out.print("ch1 : " + ch1 + "\nch2 : " + ch2);
        
    }
}

When the above Java program is compile and run, it will produce the following output:

java characters

Notice here that variable ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X. As discussed, the ASCII character set occupies the first 127 values in the Unicode character set. For this cause, all the older tricks that you may have used with characters in other languages will work in Java, also.

Even though char is designed to hold the Unicode characters, it can also be used as an integer type on which you can perform the arithmetic operations. For instance, you can add two characters together, or increment the value of a character variable. Consider the below example program :

Example

/* Java Program Example - Java Characters
*  char variables behaves like integers */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        char ch1;
        
        ch1 = 'X';
        System.out.println("ch1 contains " +ch1);
        
        ch1++;      // increments ch1
        System.out.println("ch1 is now " + ch1);
        
    }
}

When the above Java program is compile and run, it will produce the following output:

java character data types

In the above program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1 holding Y, the next character in the ASCII (and Unicode) sequence.

Escape Sequences in Java

A character preceded by the backslash (\) is an escape sequence and has particular meaning to the compiler.

As you see, the newline character (\n) has been used frequently in System.out.println() statements to advance to the next line after the string is printed.

Look at the table given here, which shows the Java's escape sequences:

Escape Sequence Description
\n Inserts a newline
\b Inserts a backspace
\f Inserts a form feed
\r Inserts a carriage return
\' Inserts a single quote character
\" Inserts a double quote character
\\ Inserts a backslash character
\t Inserts a tab

When an escape sequence is encountered in a print statement, the compiler interprets it consequently. Let's look at the following example:

If you want to put quotes within quotes then you must use the escape sequence, \", on the interior quotes:

/* Java Program Example - Java Characters */

class JavaProgram
{
    public static void main(String args[])
    {
        
        System.out.println("He said \"Hello!\" to me.");
        
    }
}

It will produce the following result:

characters in java

More Example

Here are some examples related to characters, that you can go for.

Java Online Test


« Previous Tutorial Next Tutorial »