Character Data
Total Page:16
File Type:pdf, Size:1020Kb
CHARACTER DATA A variable of the data type char can hold one keyboard character. Its value is stored in memory as a 16-bit binary code using the Unicode encoding scheme. Unicode has 65,536 codes of which approximately 35,000 are in use. It is designed to handle all characters in all written languages. Character literals are typed as single quotation marks (') delimiting a single keyboard character. Example Unicode value shown in hexadecimal char a, b, c, d, e; a 0063 a = 'c'; b 0032 b = '2'; c 006E c = 'n'; d 0025 d = '%'; e 0022 e = '"'; Inside a character literal, you sometimes need to specify a character that is either invisible (e.g. a line ending) or cannot be found on many keyboards (e.g. the symbol £). To do this, you use an escape sequence. The syntax is: \u character code character code is the 4-digit hexadecimal Unicode value denoting the desired character. Example This code fragment outputs Temperature:70°F. char degreeSymbol = '\u00B0'; int temp = 70; System.out.println("Temperature:" + temp + degreeSymbol + "F"); Example This code fragment outputs London Pizza £9.25. double price = 9.25; System.out.println( "London Pizza " + '\u00A3' + price ); Character Data Page 1 Since the ' delimits a character literal, you must use the escape character if you want the ' to stand for itself. Since the \ signals an escape character, you must use two together if you want it to stand for itself. The line ending character is the special escape sequence \n. Example Unicode value shown in hexadecimal char a, b, c; a 0027 a = '\''; b 005C b = '\\'; c 000A c = '\n'; Beginner Errors using Character Data Examples char letter = "n"; Error! " cannot delimit a character literal. System.out.print( 'x = ' ); Error! ' cannot delimit a string literal. char singleQuote = '''; Error! The compiler takes the second ' as the closing delimiter and gets confused when encountering the third '. The correct literal must be '\''. char backSlash = '\'; Error! The compiler takes \' as a ' that stands for itself. The character literal has no closing delimiter. The correct literal must be '\\'. char degreeSymbol = '/u00B0'; Error! The / is not the correct escape character. The correct literal must be '\u00B0'. char degreeSymbol = '/U00B0'; Error! U must be lower-case. Character Data Page 2 Exercises Some of these initializations are erroneous. For each, either correct the error or indicate that it is error free. 1. char a = 'A'; 2. char b = "A"; 3. char c = '''; 4. char d = '\'' 5. char e = '"'; 6. char f = '\"'; 7. char g = '\'; 8. char h = '\\'; 9. Declare c1 a char variable initialized to the dollar sign ($). 10. Declare c2 a char variable initialized to the Euro symbol (€). 11. Declare c3 a char variable initialized to the backslash (\). 12. Declare c4 a char variable initialized to the single quotation mark ('). 13. Declare c5 a char variable initialized to the double quotation mark ("). 14. Declare c6 a char variable initialized to the line ending character (\n). 15. Declare POUND_SIGN a char constant identifier initialized to the pound sign (#). 16. Declare PERCENT_SYMBOL a char constant identifier initialized to the percent symbol (%). Character Data Page 3.