The character data type char Character type char

  • Slides: 20
Download presentation
The character data type char

The character data type char

Character type char • is used to represent alpha-numerical information (characters) inside the computer

Character type char • is used to represent alpha-numerical information (characters) inside the computer • uses 2 bytes of memory to store the Unicode value

Unicode • Unicode provides a unique number for every character

Unicode • Unicode provides a unique number for every character

ASCII code (1) • The value 0 --- 127 of the Unicode is called

ASCII code (1) • The value 0 --- 127 of the Unicode is called the ASCII code • The American Standard Code for Information Interchange (ASCII) code was designed to represent characters in the English alphabet

ASCII code (2)

ASCII code (2)

Defining character typed variables (1) • Syntax to define an character typed variable: •

Defining character typed variables (1) • Syntax to define an character typed variable: • • The keyword char announces the variable definition clause The Name. Of. Variable is an identifier which is the name of the variable. The variable definition clause is must be ended with a semi-colon "; " A char typed variable can store the Unicode of one character

Character literals • A character literal is written between quotes '. . . ‘

Character literals • A character literal is written between quotes '. . . ‘

Defining character typed variables (2)

Defining character typed variables (2)

char and int (1) • A char typed variable contains a character code •

char and int (1) • A char typed variable contains a character code • A character code is a (positive) number • So: a char data type is an integer data type !!! The size of the char data type is 2 bytes The range of values of the char data type is: [0. . 65535]

char and int (2) • A char variable is an integer typed variable. •

char and int (2) • A char variable is an integer typed variable. • The expression 'A' is an integer (= 65) in a Java program !!!!

char and int (3) • Although char type is a kind of integer type,

char and int (3) • Although char type is a kind of integer type, it is treated differently when we want to "see" the value (e. g. , printing) • Different encoding methods used in printing: • When printing a char typed integer, the print and println methods will use the Unicode table to print the corresponding character. • When printing other kinds of integer types, the print and println methods will use the binary number encoding.

Converting char to int: Operations on the char data type (1) • All operations

Converting char to int: Operations on the char data type (1) • All operations on integer typed values in Java are converted to int type • the conversion from char to int is safe:

Converting char to int: Operations on the char data type (2)

Converting char to int: Operations on the char data type (2)

Converting char to int: Operations on the char data type (3)

Converting char to int: Operations on the char data type (3)

Converting other integer type into char (1) • It is always unsafe to convert

Converting other integer type into char (1) • It is always unsafe to convert any integer type (byte, short, int, and long) to char: The range of byte, short, int, and long include some negative values The range of values of char only contain positive values (including 0). We must use casting (char) to assign any kind of integer value to a char typed variable.

Converting other integer type into char (2)

Converting other integer type into char (2)

Converting other integer type into char (3) • The (same) value will now be

Converting other integer type into char (3) • The (same) value will now be interpreted using the Unicode • System. out. println(a); will print the letter 'b'.

Testing for lower case and upper case characters • How to detect a lower

Testing for lower case and upper case characters • How to detect a lower case letter: • How to detect an upper case letter:

Converting a lower case character into the same upper case character (1) • We

Converting a lower case character into the same upper case character (1) • We can convert an Unicode for a lower case letter into the same upper case letter by subtracting 32 from the code value.

Converting a lower case character into the same upper case character (2)

Converting a lower case character into the same upper case character (2)