day02-数据类型转换_自动转换的举例和顺序

This commit is contained in:
2026-07-15 15:52:05 +08:00
parent b94ba44ebe
commit 9f9a101b69

View File

@@ -0,0 +1,25 @@
package com.inmind.datatype;
/*
注意范围小的类型向范围大的类型提升byte、short、char 运算时直接提升为int
byte、short、char-->int-->long-->float-->double
*/
public class Demo02 {
public static void main(String[] args) {
//定义一个byte类型的变量
byte b = 1;
short s = 2;
//short sum = b+s;//错误此时b和s都自动提升为int型无法赋值给short型变量
int sum = b+s;
//定义长整型的变量
long l = 3L;
long l1 = l+sum;//能sum发生了自动类型转换自动转换为long型
System.out.println(l1);
//定义一个双精度的浮点型变量
double d = 3.15;
double sum1 = d+l;//能l发生了自动类型转换自动转换为double型
System.out.println(sum1);
}
}