Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory."— Presentation transcript:

1 The character data type char

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

3 Unicode Unicode provides a unique number for every character

4 ASCII code (1) The value 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

5 ASCII code (2)

6 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

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

8 Defining character typed variables (2)

9 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: [ ]

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

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

12 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:

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

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

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

16 Converting other integer type into char (2)

17 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'.

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

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

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


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

Similar presentations


Ads by Google