字符在Java中是否具有内在的int值?

前端之家收集整理的这篇文章主要介绍了字符在Java中是否具有内在的int值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么这段代码打印97?我以前没有在我的代码中的其他地方分配97到’a’.
  1. public static void permutations(int n) {
  2. System.out.print('a' + 0);
  3. }

解决方法

a的类型为char,chars可以隐式转换为int. a由97表示,因为这是小拉丁字母a的代码点.
  1. System.out.println('a'); // this will print out "a"
  2.  
  3. // If we cast it explicitly:
  4. System.out.println((int)'a'); // this will print out "97"
  5.  
  6. // Here the cast is implicit:
  7. System.out.println('a' + 0); // this will print out "97"

第一个调用调用println(char),其他调用调用println(int).

相关:In what encoding is a Java char stored in?

猜你在找的Java相关文章