加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:20:51 所属栏目:Java 来源:网络整理
导读:为什么这段代码打印97?我以前没有在我的代码中的其他地方分配97到’a’. public static void permutations(int n) { System.out.print('a' + 0);} 解决方法 a的类型为char,chars可以隐式转换为int. a由97表示,因为这是小拉丁字母a的代码点. System.out.prin
为什么这段代码打印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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读