In Java, characters are not 8-bit quantities like they are in most other computer languages.Instead, Java uses Unicode. Unicode defines a character set that can represent all of the characters found in all human languages. Thus, in Java,char is an unsigned 16-bit type having a range of 0 to 65,536. The standard 8-bit ASCII character set is a subset of Unicode and ranges from 0 to 127. Thus, the ASCII characters are still valid Java characters. A character variable can be assigned a value by enclosing the character in single quotes. For example, this assigns the variable ch the letter X:
char ch; ch = 'X';
You can output a char value using a println( )statement. For example, this line outputs the value inch:
System.out.println("This is ch: " + ch);
Since char is an unsigned 16-bit type, it is possible to perform various arithmetic manipulations on a char variable. For example, consider the following program:
// Character variables can be handled like integers.
class Char ArithDemo
{
public static void main(String args[])
{
char ch;
ch = 'X'; System.out.println("ch contains " + ch);
ch++;
// increment ch
System.out.println("ch is now " + ch);
ch = 90;
// give ch the value Z
System.out.println("ch is now " + ch);
}
}
Friday, December 14, 2018
Java Characters Data Types
December 14, 2018
No comments
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment