| Author |
Dans mock - char escape sequences
|
alex earnshaw
Ranch Hand
Joined: Nov 05, 2001
Posts: 60
|
|
In Study Guide Mock, Chapter 2, Exam A:
Question 7 1. class Green { 2. public static void main ( String[] args) { 3. char a = '\b'; 4. char b = '\c'; 5. char c = '\d'; 6. char d = '\f'; 7. char e = '\l'; 8. char f = '\n'; 9. char g = '\r'; 10. char h = '\t'; 11. char i = '\\'; 12. char j = '\"'; 13. char k = '\''; 14. } 15. } What is the result of attempting to compile and run the program? a. Compiler error at line 3. b. Compiler error at line 4. c. Compiler error at line 5. d. Compiler error at line 6. e. Compiler error at line 7. f. Compiler error at line 8. g. Compiler error at line 9. h. Compiler error at line 10. i. Compiler error at line 11. j. Compiler error at line 12. k. Compiler error at line 13. l. None of the Above
Answer is given as b,c,e. I don't understand, I thought only unicode was processed early i.e. '\u000d' would become carriage return and cause compile error - is '\d' the same thing? Also what is '\l' and why does this cause compile error? Can anyone give me a full list of chars which cause compile error.... Thanking you, Alex
|
 |
John Paverd
Ranch Hand
Joined: Nov 17, 2002
Posts: 115
|
|
From JLS section 3.10.6:
EscapeSequence: '\b' /* \u0008: backspace BS */ '\t' /* \u0009: horizontal tab HT */ '\n' /* \u000a: linefeed LF */ '\f' /* \u000c: form feed FF */ '\r' /* \u000d: carriage return CR */ '\"' /* \u0022: double quote " */ '\'' /* \u0027: single quote ' */ '\\' /* \u005c: backslash \ */ It is a compile-time error if the character following a backslash in an escape is not an ASCII b, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6, or 7.
\d and \l are not valid because they: are not one of the above escape sequences are not unicode values (do not start with \u) are not octal values (do not start with \digit).
|
SCJP 1.4
|
 |
Steve Leung
Greenhorn
Joined: Dec 05, 2002
Posts: 3
|
|
Hello, I wonder to know that what is the result of char d = '\0'; Is it the same as char d = '0' ; ? Moreover, is it still the type char for both case ? Rgds, Steve
|
SCJP 1.4
|
 |
alex earnshaw
Ranch Hand
Joined: Nov 05, 2001
Posts: 60
|
|
Originally posted by John Paverd: \d and \l are not valid because they: are not one of the above escape sequences are not unicode values (do not start with \u) are not octal values (do not start with \digit
Thanks John! So just to clarify: It is a compile error if the character following a backslash is not an ASCII b, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6, or 7 It is also a compile error if a char is initialised to '\u000a' '\u000c' or '\u000d' as these are processed early resulting in a new line being started, giving The equivalent escape sequences '\n', '\f' and '\r' compile okay as they are not processed early. And the unicodes for backspace, tab etc all compile okay as they do not cause a new line to be started even though they are processed early. Is this correct? Have I got it right now? Alex
|
 |
John Paverd
Ranch Hand
Joined: Nov 17, 2002
Posts: 115
|
|
Originally posted by alex earnshaw: And the unicodes for backspace, tab etc all compile okay as they do not cause a new line to be started even though they are processed early. Alex
Alex I agree with your explanation of the processing of unicode literals and escape sequences. As a good programming practice, I would always use the escape sequences instead of the Unicode values. I was only able to use \u0008, \u0009, \u000c and \u0022 in a char literal without compilation error. I have not taken the exam yet, so maybe someone else can comment on whether we need to recognize the unicode literals corresponding to the escape sequences for the exam. [ December 05, 2002: Message edited by: John Paverd ] [ December 05, 2002: Message edited by: John Paverd ]
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
Originally posted by Steve Leung: Hello, I wonder to know that what is the result of char d = '\0'; Is it the same as char d = '0' ; ? Moreover, is it still the type char for both case ? Rgds, Steve
The first of the two, '\0', is an octal escape sequence with the numeric value of zero. It is the same as '\u0000'. It represents the NULL character. The second of the two, '0', has a numeric value of 48 and represents the character 0.
|
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
|
 |
John Paverd
Ranch Hand
Joined: Nov 17, 2002
Posts: 115
|
|
Originally posted by Steve Leung: Hello, I wonder to know that what is the result of char d = '\0'; Is it the same as char d = '0' ; ? Moreover, is it still the type char for both case ? Rgds, Steve
Gives the result: 0 48 0 d, e, f are all declared as type char, and will remain as type char, regardless of what type of value is assigned to them. For example, in f = 0, 0 is an int, but the int literal 0 is converted to the equivalent char value at compile time.
|
 |
alex earnshaw
Ranch Hand
Joined: Nov 05, 2001
Posts: 60
|
|
Originally posted by John Paverd: As a good programming practice, I would always use the escape sequences instead of the Unicode values. I was only able to use \u0008, \u0009, and \u0022 in a char literal without compilation error.
I decided to try this out for myself...can anyone explain the result for \u000c - why does it NOT cause compile error? and why \r seems to print a backspace rather than a carriage return? For the purpose of the exam which should we say will cause a compile error???
|
 |
John Paverd
Ranch Hand
Joined: Nov 17, 2002
Posts: 115
|
|
Alex, you are correct, char c = '\u000c'; does compile. I have edited my original post to reflect this. I hope that someone can tell us what part of our discussion, if any, is relevant to the exam.
|
 |
John Paverd
Ranch Hand
Joined: Nov 17, 2002
Posts: 115
|
|
Originally posted by alex earnshaw: For the purpose of the exam which should we say will cause a compile error???
Because the characters \u000d (CR) and \u000a (LF) are recognized by the Java compiler as line terminators, and not input characters, they will always cause compile errors if you attempt to embed them in string or character literals. Other Unicode escapes may cause a compile error, depending on how they are used. For an example, see the following code, with my original failed attempt commented out.
|
 |
 |
|
|
subject: Dans mock - char escape sequences
|
|
|