Jigar Patel Jigar Patel - 2 months ago 7
Java Question

What would be the value of variable after executing the following statement

I'm lil bit confused about this question which i found while doing 1 vasic java quiz

question is:
What would be the value of variable c after executing

int c = Integer.parseInt("a");


I think answer would be 97, which is ASCII value of "a"

am I correct?

Answer

It throws a NumberFormatException. From the Javadoc on Integer.parseInt(String s):

Throws:
NumberFormatException - if the String does not contain a parsable int.

Retrieving the ASCII value of a character is done like this:

char ch='a';
System.out.println((int)ch);
Comments