Presentation is loading. Please wait.

Presentation is loading. Please wait.

The character data type char

Similar presentations


Presentation on theme: "The character data type char"— Presentation transcript:

1 The character data type char

2 Character type char The char data type:
is a built-in (primitive) data type of Java is used to represent alpha-numerical information (characters) inside the computer uses the Unicode to encode characters from many different kinds of languages in the world uses 2 bytes of memory to store the Unicode value The Unicode includes the ASCII code which is used to encode English characters

3 Character type char (cont.)
Unicode: Official website for Unicode: click here Unicode provides a unique number for every character Example: The number 65 of Unicode represents the (English) character A The number 35 of Unicode represents the character # The number 948 of Unicode represents the (Greek) character δ The number of Unicode represents the (Chinese) character 張

4 Character type char (cont.)
ASCII code: 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 Character type char (cont.)
ASCII code: (with letters and number highlighted) 0 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL 8 BS 9 HT NL VT NP CR SO SI 16 DLE 17 DC DC DC DC NAK 22 SYN 23 ETB 24 CAN 25 EM SUB 27 ESC 28 FS GS RS US 32 SP 33 ! " # $ % & ' 40 ( ) * , / : ; < = > ? A B C D E F G 72 H I J K L M N O 80 P Q R S T U V W 88 X Y Z [ \ ] ^ _ 96 ` a b c d e f g 104 h i j k l m n o 112 p q r s t u v w 120 x y z { | } ~ DEL

6 Character type char (cont.)
Here is an ASCII code table in hexadecimal, decimal and binary numbers

7 Character literals We write numerical literals as follows: 123 3.1415
(And so on)

8 Character literals (cont.)
We cannot write character literals as follows: because: A a (And so on) They can be confused with identifiers !!! (E.g., names of variables)

9 Character literals (cont.)
A character literal is written between quotes '...' Examples: 'A' is the character literal A (ASCII code value 65) 'a' is the character literal a (ASCII code value 97) '1' is the character literal 1 (ASCII code value 49)

10 Defining character typed variables
Syntax to define an character typed variable: Notes: char NameOfVariable ; 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

11 Defining character typed variables (cont.)
Example: public class Char01 { public static void main(String[] args) char a; a = 'b'; // a = ASCII code of 'b' (98) System.out.println(a); // Print a using ASCII code }

12 Defining character typed variables (cont.)
Example Program: (Demo above code) Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac Char01.java To run:          java Char01

13 Operations on the char data type
No arithmetic operations: The only available operations are ++ and --. We do not add, subtract, multiply or divide one character with another character... There are no arithmetic operations defined on the char data type

14 Operations on the char data type (cont.)
The ++ and -- operations applied on char typed variables: The ++ operation applied on a char typed variable will increment the (Unicode) value The variable will contain the (Unicode) value of the next character The -- operation applied on a char typed variable will increment the (Unicode) value The variable will contain the (Unicode) value of the previous character

15 Operations on the char data type (cont.)
Example: public class Char02 { public static void main(String[] args) char a; a = 'b'; System.out.println(a); // Prints b a++; // "Next" character System.out.println(a); // Prints c }

16 Operations on the char data type (cont.)
Explanation: The variable a first contains the Unicode for the character 'b' which is equal to 98: The statement a++; increments the variable a to the new value 99 which is the code for the character 'c' That's why when variable a is printed, the letter 'c' is printed. ..... (portion of the ASCII code table) .... 88 X Y Z [ \ ] ^ _ 96 ` a b c d e f g 104 h i j k l m n o .....

17 Operations on the char data type (cont.)
Example Program: (Demo above code) Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac Char02.java To run:          java Char02

18 Exercise What will the following program print: public class Char02a {
public static void main(String[] args) char a; a = '%'; System.out.println(a); // Prints % a++; // "Next" character System.out.println(a); // What will this statement print ? }

19 Exercise (cont.) Hint: use the ASCII table

20 Exercise (cont.) Answer: &

21 Converting a number type into char
Java allows you to convert a numerically typed (integer, short, etc) into the char type.

22 Converting a number type into char (cont.)
Example: public class Char03 { public static void main(String[] args) int x; char a; x = 98; // 98 is the code for 'b' // a = x; // This is not allowed without casting a = (char) x; // Convert int to char System.out.println(a); // Prints b }

23 Converting a number type into char (cont.)
Explanation: The variable x is assigned the value 98 (which is the Unicode for the character 'b'): (The bit pattern encodes 98 using the binary number system

24 Converting a number type into char (cont.)
The statement a = (char) x; will achieve the following: This statement copies the value from an int typed variable into a char typed variable.

25 Converting a number type into char (cont.)
Effect: That's why System.out.println(a); will print the letter 'b'. The (same) value will now be interpreted using the Unicode code (E.g.: The pattern is equal to 98 and according to the Unicode scheme, it is the code for the letter 'b')

26 Converting a number type into char (cont.)
Example Program: (Demo above code) Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac Char03.java To run:          java Char03

27 Converting a char typed value into an integer
We can also do the reverse: Convert a char typed value into an integer

28 Converting a char typed value into an integer (cont.)
Example: public class Char04 { public static void main(String[] args) int x; char a; a = '%'; // ASCII code of '%' = 37 x = a; // Copy 37 into x // We don't need to use (int) // is is "safe" (int is bigger than char) System.out.println(x); // Prints 37 }

29 Converting a char typed value into an integer (cont.)
Explanation: The variable a is assigned the Unicode value for the character % (which is the number 37): (The bit pattern encodes the number 37 and it is the Unicode for the character '%'

30 Converting a char typed value into an integer (cont.)
The statement x = a; will achieve the following: This statement copies the value from a char typed variable into a int typed variable.

31 Converting a char typed value into an integer (cont.)
Effect: That's why System.out.println(x); will print the number 37. The (same) value will now be interpreted using the binary number system (E.g.: The pattern is equal to 37

32 Converting a char typed value into an integer (cont.)
Example Program: (Demo above code) Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac Char04.java To run:          java Char04


Download ppt "The character data type char"

Similar presentations


Ads by Google