JAVA入门————Day04(数据类型转换)

数据类型转换:

数据类型转换:
自动类型转换:小类型转换为大类型
强制类型转换:大类型转换为小类型

(注:double>float>long>int>short>byteint c=7byte d=byte)c;
特殊情况:	
//溢出
		int c=210000000;
		byte d=(byte)c;//(-128~127)
		System.out.println(d);
//精度丢失
		double c=12.9;
		int d=(int)c;//(int后面是不能赋值小数的所以d的结果应该是12);
		System.out.println(d);
规则:
//1.整数能直接赋值给byte,short,char但不能超范围。
		char x=65;
		System.out.print (x);
		byte y='A';
		System.out.println(y);
console: A 65 (ASCII码)
	//2.运算过程中byte,short会将最终的运算结果转换成int
		byte i=6;
		byte j=7;
		byte k=(byte)(i+j);//因为i j在运算过程中系统会自动转换成int类型进行运算,所以若要赋值给byte类型的k就得用到强制转换
		System.out.println(k);

猜你喜欢

转载自blog.csdn.net/qq_54177999/article/details/114285971
今日推荐