Java Data Type Tutorial - Java char Data Type








The char data type is a 16-bit unsigned Java primitive data type. It represents a Unicode character.

Note that char is an unsigned data type. Therefore, a char variable cannot have a negative value.

The range of the char data type is 0 to 65535, which is the same as the range of the Unicode set.

Character literal

A character literal represents a value of the char data type. A character literal in Java can be expressed in the following formats:

  • A character enclosed in single quotes
  • As a character escape sequence
  • As a Unicode escape sequence
  • As an octal escape sequence

A character can be expressed by enclosing it in single quotes.

The following snippet of code assigns values to char variables using this form:

char   c1  = 'A'; 
char   c2  = 'L'; 
char   c3  = '5'; 
char   c4  = '/';

A sequence of characters enclosed in double quotes is a String literal.

A String literal cannot be assigned to a char variable, even if the String literal has only one character.





Character escape sequence

A character literal can be expressed as a character escape sequence.

A character escape sequence starts with a backslash immediately followed by a character, and both are enclosed in single quotes.

There are eight predefined character escape sequences as listed in the following table.

Character Escape SequenceDescription
'\n' A linefeed
'\r'A carriage return
'\f'A form feed
'\b'A backspace
'\t' A tab
'\\'A backslash
'\"'A double quote
'\''A single quote

There are only eight character escape sequences in Java. We cannot define your own character escape sequences.

char   c1  = '\n'; // Assigns a linefeed to c1 
char   c2  = '\"'; // Assigns double quote to c2
char   c3  = '\a'; // A compile-time error. Invalid character escape sequence




Unicode

A character literal can be expressed as a Unicode escape sequence in the form '\uxxxx'.

Here, \u denotes the start of the Unicode escape sequence, and xxxx represents exactly four hexadecimal digits.

The value represented by xxxx is the Unicode value for the character.

The character 'A' has the Unicode value of 65. The value 65 in decimal can be represented in hexadecimal as 41.

So, the character 'A' can be expressed in Unicode escape sequence as '\u0041'.

The following code assigns the same character 'A' to the char variables c1 and c2:

char c1  = 'A';
char c2  = '\u0041'; // Same as  c2  = 'A'

A character literal can be expressed as an octal escape sequence in the form '\nnn'.

Here, n is an octal digit (0-7). The range for the octal escape sequence is '\000' to '\377'.

The octal number 377 is the same as the decimal number 255. Therefore, using octal escape sequence, we can represent characters whose Unicode code range from 0 to 255 decimal integers.

A Unicode character set (code range 0 to 65535) can be represented as a Unicode escape sequence ('\uxxxx').

We can assign an int literal to a char variable if int literal falls in the range 0-65535.

When assigning an int literal to a char variable, the char variable represents the character whose Unicode code is equal to the value represented by that int literal.

The Unicode code for the character 'a' is 97. The decimal value 97 is represented as 141 in octal and 61 in hexadecimal.

The following code shows how to use int literal 97 to represent the Unicode character 'a'.

char   c1  = 97;  // Same as  
c1  = 'a'; 
c1  = '\141'; 
c1  = '\u0061';

Example

Here is a program that demonstrates char variables:

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

ch1 is assigned the value 88, which is the ASCII and Unicode value that corresponds to the letter X. ASCII character set occupies the first 127 values in the Unicode character set.

The code above generates the following result.

Example 2

char is designed to hold Unicode characters, but it can also be used as an integer type on which you can perform arithmetic operations.

For example, you can add two characters together, or increment the value of a character variable.

public class Main {
  public static void main(String args[]) {
    char ch1;//from   w  w w. j a va 2  s .com

    ch1 = 'X';
    System.out.println("ch1 contains " + ch1);

    ch1++; // increment ch1
    System.out.println("ch1 is now " + ch1);
  }
}

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

The code above generates the following result.