1 / 32

The character data type char

The character data type char. 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

kylia
Download Presentation

The character data type char

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 representalpha-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 Unicodeincludes 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 24373 of Unicode represents the (Chinese) character 張

  4. Character type char (cont.) • ASCII code: • The value 0 --- 127 of the Unicode is called the ASCII code • The American Standard Code for Information Interchange (ASCII) code was designed to representcharacters 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 10 NL 11 VT 12 NP 13 CR 14 SO 15 SI 16 DLE 17 DC1 18 DC2 19 DC3 20 DC4 21 NAK 22 SYN 23 ETB 24 CAN 25 EM 26 SUB 27 ESC 28 FS 29 GS 30 RS 31 US 32 SP 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 DEL

  6. Character type char (cont.) • Here is an ASCII code table in hexadecimal, decimalandbinary 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.) • Character literals: • 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 keywordchar 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: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/Char01.java • 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 notadd, subtract, multiply or divide one character with another character... • There are noarithmetic 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 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o .....

  17. Operations on the char data type (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/Char02.java • 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 00000000 00000000 00000000 01100010 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 00000000 01100010 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: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/Char03.java • 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 00000000 00100101 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 00000000 00000000 00000000 00100101 is equal to 37

  32. Converting a char typed value into an integer (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/Char04.java • How to run the program:             • Right click on link and save in a scratch directory • To compile:   javac Char04.java • To run:          java Char04

More Related