1、字符的混合运算:
class DataTypeConversion{
public static void main(String[] args){
System.out.println('a');
}
}
上面代码输出结果:a
class DataTypeConversion{
public static void main(String[] args){
System.out.println('a' + 1);
}
}
上面代码输出结果:98
分析:因为在计算的过程中char
类型会自动提升为int
类型进行计算,输出结果为98
,'a' + 1 = 98
,那么就说明char
类型的'a'
转换为int
是97
,那么为什么'a'
进行混合运算的时候会提升为97呢?
2、ASCII编码表:
计算机只识别0
和1
,为了方便把a,b,c,1,2,3...
等等这些字符用二进制进行表示,所以就制作了ASCII,所有人统一遵守ASCII的使用规则,在ASCII编码表中'a'
字符对应的就是int
类型的97
,所以在混合运算时会将a
转换为97
进行运算。
3、字符串的拼接:
任何数据类型用+
于字符串相连接都会产生新的字符串。
class DataTypeConversion{
public static void main(String[] args){
System.out.println("hello" + 'a' + 1);
System.out.println('a' + 1 + "hello");
}
}
输出结果:helloa1 和 98hello