您的位置:首页 > 编程语言 > Java开发

java char 和 int 向上转型

2015-08-08 17:14 435 查看
char 占2个字节

范围为

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


输出为

[code]a


如果这样

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


输出为

[code]97


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

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


输出为

[code]a0


char + String 一起, char会被转为String类型输出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: