JAVA基础学习笔记 day002_基本数据类型间的转换

public class BaseClassChangeDemo {

	public static void main(String[] args) {
		//基本数据类型间的转换:自动类型转换&强制类型转换
		/*
		 * 自动类型转换:小类型转换成大类型
		 */
		int a = 100;
		long b = a;
		
		/*
		 * 强制类型转换:大类型转换成小类型
		 * 变量的强转:可能会造成变量内部存储数值的溢出或者精度丢失
		 */
		long l = 100L;
		int i = (int)l;
		
		//两个特殊的点
		/*
		 * 1.整数类型直接量,可以直接赋值给byte,short,char,但不能超出范围
		 */
		byte by = 100;
		/*
		 * 2.有且只有byte,short,char这三种类型的数据参与运算的时候,先将他们
		 * 	统一自动类型转换成 int,再参与计算
		 */
		byte by1 = 1;
		short by2 = 11;
		byte by3 = (byte)(by1 + by2);
		System.out.println(by3);
		
	}

}

猜你喜欢

转载自blog.csdn.net/qq_42801561/article/details/87878102