This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by Chiran Mathur: The numeric value for the letter 'a' is 97. Which of these code fragments will successfully declare and initialize a variable of type 'char' with this value?
Select 2 correct options a char ch = 'a';
b char ch = '\97'; It will give illegal escape sequence.
c char ch = '\u0097'; '\u' is used for Unicode values and '97' is not the unicode value of 'a'. ( Unicode value of 'a' is '61', which is actually '97' in decimal)
d char ch = 0x97; 0x means it is a hex number so It will make it 151.
e char ch = 97;
The answer is a and e. Can someone explain this?
Chiran, A char can broadly considered as a numeric type according to JLS
The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (�4.2) are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double
SO a character can be declared in one of the following ways
char c = 'a' (if you are declaring the exact character , use single quotes)
char c = 97 (as char is a numeric type, you can also declare char c to be the ASCII value of 'a')
char c = '\uxxxx' where xxxx is the hexadecimal equivalent of the character's value , here it is 97...therefor its hex is 0x61 ...so the character can be '\u0061...the 0x part from hex is removed )
Hope you got my answer Sri
Joined: Feb 07, 2001
So does one have to remember the ascii value of each character