java char 和 int 向上转型

来源:互联网 发布:string数组 添加 编辑:程序博客网 时间:2024/06/16 17:33

char 占2个字节

范围为

public class TestChar {    public static void main(String[] args) {        char testChar = 'a';        System.out.println(testChar);    }}

输出为

a


如果这样

public class TestChar {    public static void main(String[] args) {        char testChar = 'a';        System.out.println(testChar + 0);    }}

输出为

97

这是因为char + int , char 会被自动向上转型为int, ‘a’ 的 ACSII 码为97



public class TestChar {    public static void main(String[] args) {        char testChar = 'a';        System.out.println(testChar + "0");    }}

输出为

a0

char + String 一起, char会被转为String类型输出

0 0
原创粉丝点击