Sunday, May 01, 2005

Escape character trong Java

Có một câu hỏi thi thử như thế này:
class EscapeCharacter {
public static void main (String[] args) {
char a = '\c'; // 1
char b = '\r'; // 2
char c = '\"'; // 3
char d = '\b'; // 4
char e = '\''; // 5
}
}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above

Dấu backslash dùng để chỉ kí tự thoát cho một số character đặc biệt mà thôi. Dùng cho kí tự khác nó sẽ báo lỗi ngay.
illegal escape character

Ặc ặc, ở đây họ chơi mẹo. Câu trả lời là a vì c không phải là kí tự để có thể dùng escape character được.

The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
Hôm nay được một bài học.

1 Comments:

At 10:24 AM, Anonymous kevin said...

Quả là một cú lừa tình rất hay

 

Post a Comment

<< Home