Java 字符和字符串参与运算

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'转换为int97,那么为什么'a'进行混合运算的时候会提升为97呢?

2、ASCII编码表:

计算机只识别01,为了方便把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

猜你喜欢

转载自blog.csdn.net/weixin_44296929/article/details/106904763