Traditionally, in most of the computer languages char data type was used to be an unsigned 8-bit ASCII character, whose value ranges from 0 to 127. But the char data type in Java is an unsigned 16-bit character, which represents the Unicode character set and its value ranges from 0 to 65,535. ASCII values are still valid characters and are subset of Unicode characters. Unicode is a standard character set that represents all the world's languages. So, usage of Unicode character set made Java more portable.
I have already discussed about char data type in my earlier posts - char Data Type, How to get characters from console, Stack implementation using char Array, How to reverse the string using char Array, A character used in switch-case statement.
There is one more feature of the char data type in Java, it can be treated as an integer and can have arithmetic operations on it. It is clear in the following example -
Class Test (Test.java)
public class Test {
public static void main (String args[]) {
char ch = 'A';
System.out.println("Character is " + ch); // 'A'
ch++; // increment by 1
System.out.println("Character is " + ch); // 'B'
ch+=5; // increment by 5
System.out.println("Character is " + ch); // 'G'
ch--;
System.out.println("Character is " + ch); // 'F'
ch-=3;
System.out.println("Character is " + ch); // 'C'
// ch = ch + 10; doesn't convert implicitly, needs explicit type conversion
// Explicit Type Conversion
ch = (char)(ch + 10);
System.out.println("Character is " + ch); // 'M'
ch = (char)(ch - 5);
System.out.println("Character is " + ch); // 'H'
ch = 75;
System.out.println("Character is " + ch); // 'K'
ch = 225;
System.out.println("Character is " + ch); // 'á'
}
}
Output:
Character is A
Character is B
Character is G
Character is F
Character is C
Character is M
Character is H
Character is K
Character is á
When the increment operator (++) is used on the char variable ch, implicitly the character is converted to its integer unicode value and then adds it to 1. Then it again converts back to char data type implicitly.
ch++;
Same happens with other shorthand operators --, +=, -= or assigning literal value of an integer like in the following statement -
ch--;
ch+=5;
ch-=3;
ch = 75;
ch = 225;
But, when we do simple arithmetic operations, the integer value of the variable is not converted into char data type implicitly. We need to convert the int value to character value explicitly, When we convert one data type variable to another data type variable, it is called Type Casting. Here are the statements using casting from int to char data type -
ch = (char)(ch + 10);
ch = (char)(ch - 5);
In the above statements, value of character variable ch is converted to an int data type implicitly. Then addition or subtraction happens. But, the result value is an int data type and it can not be assigned directly to a char variable. So, explicit casting is done by adding word (char) to the statement. I have already used Type Casting in my earlier post How to get the character input from console.
NOTE: We will discuss Type Casting in coming posts.