gpt4 book ai didi

java - chars 在 Java 中有内在的 int 值吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:42 25 4
gpt4 key购买 nike

为什么这段代码打印出 97?我之前没有在我的代码中的任何其他地方将 97 分配给“a”。

public static void permutations(int n) {
System.out.print('a' + 0);
}

最佳答案

achar 类型,chars 可以隐式转换为 inta 由 97 表示,因为这是 small latin letter 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?

关于java - chars 在 Java 中有内在的 int 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27698201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com