• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt on character arithmetic  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output for the above code is as follows

15
0
-41


How does -41 come out as result of third print statement?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:


The ASCII and Unicode value of the character '0' is 48.

And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.
 
author
Marshal
Posts: 23441
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:
And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.



Jeff,

How did you come to this conclusion? The question wasn't directly related to that. Or didn't seem to be.

Henry
 
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How does -41 come out as result of third print statement?


And what did you expect the result to be?

I suppose Mr Verdegan wanted to point OP to the difference between these two:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Jeff Verdegan wrote:
And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.



Jeff,

How did you come to this conclusion? The question wasn't directly related to that. Or didn't seem to be.

Henry



The subject "character arithmetic" combined with the fact that he used that method and was confused about the results.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajiv Rai wrote:

The output for the above code is as follows

15
0
-41


How does -41 come out as result of third print statement?



15 - I don't understand that. Hope someone might comment.

0 because Character.getNumericValue() function extracts the integer from the character. So '0' is of course zero.

'0' is ascii value 48 decimal (or 0x30) - see http://www.asciitable.com/

therefore 7 - 48 = -41

But I am puzzled with the 15 result.


EDIT

Just looked again at ascii chart. decimal 102 is character f. In hex this can be converted to decimal 15 - so there is that answer.
 
Henry Wong
author
Marshal
Posts: 23441
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Comber wrote:
15 - I don't understand that. Hope someone might comment.



When the method is given a character that is a letter (meaning 'A' to 'Z' or 'a' to 'z'), it assumes that the number is for a base that has that letter. In this case, a character with an ASCII value of 102 is the letter 'f'. And for a base 16 number, or any base higher than base 16, the character 'f' represents the number 15.

Henry

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!