hi all it is a killer question asked in a mock. Which of the following declarations will give a compiler error (Select two correct answers): a. char c = '\u000a'; b. char c = '\u000b'; c. char c = '\u000c'; d. char c = '\u000d'; answer : a , d can any body explain why? even following also compiles. char c = '\u000e'; char c = '\u000f';
Sony, They are character literals that represent escape sequences. '\u000A' is a newline '\u000D' is return I would think your choice c would yield an error as well since '\u000C' is a form feed Other ones to watch out for '\u0009' tab, '\u0008' backspace, '\u005C' backslash itself, '\u0027' single quote, '\u0022' double quote Hope this helps! Ian
Hi, Are we expected to be familiar with the unicode representation of the common escape sequences as far as preparing for the SCJP is concerned ? Does this come under any specified objective ? I hope some of the SCJPs can answer this question. Thanks
These characters are turned into their actual representation during the compile. So the new line character actually inserts a new line into your code which causes the compile error. Quote usage: single quotes are used for chars and double quotes for Strings. Slash usage: / = just another character with no special meaning \ = escape character used to represent special stuff like the new line character, the tab character, etc. \n = new line; \t = tab; etc. \\ is used because a single \ means "the next character is an escape character" so when you need a \ in your string, you have to specify two of them.
n a Java program, if the Unicode escape sequence \u000A, for example, occurs within a single-line comment, it is interpreted as a line terminator (Unicode character 000A is line feed) and therefore the next character is not part of the comment. Similarly, if the Unicode escape sequence \u000A occurs within a string literal in a Java program, it is likewise interpreted as a line terminator, which is not allowed within a string literal. One must write \n instead of \u000A to cause a line feed to be part of the string value of a string literal. Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed and the linefeed becomes a LineTerminator, and so the character literal is not valid. Instead, one should use the escape sequence '\n. Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.