Converting characters to integers in Java

There are a few ways to convert a character to an integer in Java. Here are a couple of options:

  1. Using the Character.getNumericValue method:
char c = '5';
int i = Character.getNumericValue(c);
  1. Subtracting '0' from the character:
char c = '5';
int i = c - '0';
  1. Using the Character.digit method:
char c = '5';
int i = Character.digit(c, 10);

Note that these methods will only work for characters that represent decimal digits (0-9). If you try to convert a character that is not a decimal digit, these methods will return a value between -1 and -9, depending on the character.