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. Since Java is designed to allow applets to be written for worldwide
use, it makes sense that it would use Unicode to represent characters.
Example to demonstrate Char variable
// Demonstrate char data type.
public class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 =65; //code for A
ch2 ='B';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + ", " + ch2);
}
}
Output
ch1 and ch2: A, B
Notice that ch1 is assigned the value 65, which is the ASCII (and Unicode) value that
corresponds to the letter A.Even though chars are not integers, in many cases you can operate on them as if
they were integers. This allows you to add two characters together, or to increment
the value of a character variable. For example,
consider the following program:
public class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'A';
System.out.println("ch1 contanis :"+ch1);
ch1++; //increment ch1
System.out.println("ch1 is now : " + ch1);
}
}
Output
ch1 contanis :A
ch1 is now : B
0 Comments