the character data type char. character type char is used to represent alpha-numerical information...

20
The character data type char

Upload: angelica-holmes

Post on 14-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

The character data type char

Page 2: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Character type char

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

• uses 2 bytes of memory to store the Unicode value

Page 3: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Unicode

• Unicode provides a unique number for every character

Page 4: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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

Page 5: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

ASCII code (2)

Page 6: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Defining character typed variables (1)

• Syntax to define an character typed variable:

• The keyword char announces the variable definition clause

• The NameOfVariable 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

Page 7: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Character literals

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

Page 8: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Defining character typed variables (2)

Page 9: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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]

Page 10: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

char and int (2)

• A char variable is an integer typed variable.

• The expression 'A' is an integer (= 65) in a Java program !!!!

Page 11: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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.

Page 12: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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:

Page 13: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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

Page 14: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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

Page 15: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

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.

Page 16: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Converting other integer type into char (2)

Page 17: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Converting other integer type into char (3)

• The (same) value will now be interpreted using the Unicode code

• System.out.println(a); will print the letter 'b'.

Page 18: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Testing for lower case and upper case characters

• How to detect a lower case letter:

• How to detect an upper case letter:

Page 19: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Converting a lower case character into the same upper case character

(1)• We can convert an Unicode code for a lower case letter into

the same upper case letter by subtracting 32 from the code value.

Page 20: The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory

Converting a lower case character into the same upper case character

(2)