Reputation: 1110
I am learning java and the answer of one question seems to be wrong: question: Which of the following are legal?
char c = 0x1234;//A
char c = \u1234;//B
char c = '\u1234';//C
in the book the answer is C, but I think it should be both A and C. Anyone please verify that for me?
Upvotes: 2
Views: 2246
Reputation: 279950
Both A and C are correct
char a = 0x1234;
The literal 0x1234
is a hex integer literal. Its value fits in the char
primitive type, so it is valid;
char b = \u1234;//B
Is not valid notation and so won't compile.
char c = '\u1234';
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
So it's valid.
Upvotes: 6
Reputation: 8347
char c = 0x1234;//A -- Correct
as it's value begins with 0X it is a valid hexadecimal value. Hence Correct.
char c = \u1234;//B --- Incorrect
Not a valid u code, as it's not surrounded with''.
char c = '\u1234';//C -- Correct
it is valid u code and is correctly surrounded by '', hence it is correct too.
Upvotes: 1