为什么这段代码打印97?我以前没有在我的代码中的其他地方分配97到’a’.
- public static void permutations(int n) {
- System.out.print('a' + 0);
- }
解决方法
a的类型为char,chars可以隐式转换为int. a由97表示,因为这是小拉丁字母a的代码点.
- System.out.println('a'); // this will print out "a"
- // If we cast it explicitly:
- System.out.println((int)'a'); // this will print out "97"
- // Here the cast is implicit:
- System.out.println('a' + 0); // this will print out "97"