Java char Type

Java char Type


Char Type In Java
char variables, char type is used to declare a character type of the variable by putting char keyword before an identifier.
The char data type is used to store a single character.
The character must be surrounded by single quotes, like ‘a’, ‘A’.
char num = ‘a’
char a = ‘b’;
System.out.println(num);
System.out.println(a);
As we know char is a data type and hence it will always be written in small cases otherwise it will through an error.
In Java, the data type used to store characters is char. However, C/C++ programmers beware: char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide.
This is not the case in Java. Instead, Java uses Unicode to represent characters.
Unicode defines a fully international character set that can represent all of the characters found in all human languages. 
It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. 
Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
Now we will see examples - 
Example -  Here is a program that demonstrates char variables -
// Demonstrate char data type.
class CharDemo {
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);
}
}

 
Output - 
The above program will produce the following output -
 
ch1 contains X

ch1 is now Y

 
Explanation - 
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.