字符在Java中是否具有内在的int值?
发布时间:2020-05-25 14:13:36 所属栏目:Java 来源:互联网
导读:为什么这段代码打印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); // t
|
为什么这段代码打印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"
第一个调用调用println(char),其他调用调用println(int). 相关:In what encoding is a Java char stored in? (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
