aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Numerice value for letter 'a' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Numerice value for letter Watch "Numerice value for letter New topic
Author

Numerice value for letter 'a'

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
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?
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
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
    Chiran Mathur
    Ranch Hand

    Joined: Feb 07, 2001
    Posts: 63
    So does one have to remember the ascii value of each character
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    No, they have told you it is 97 (implicitly decimal). You should however be able to convert that 97 into hexadecimal, octal, and binary.


    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Numerice value for letter 'a'