Java char和int类型数值计算
- int + char = int值 + char转ascii码的值
- int - char = int值 - char转ascii码的值
- char + char = char转ascii码的值 + char转ascii码的值
- char - char = char转ascii码的值 - char转ascii码的值
所以把一个char字符转int值,可以用该char字符减去字符'0'
public class Test {
public static void main(String[] args) {
System.out.println(9 + '0'); // 57
System.out.println(9 - '0'); // -39
System.out.println((int)'9'); // 57
System.out.println((int)'0'); // 48
System.out.println('9' + '0'); // 105
System.out.println('9' - '0'); // 9
}
}