day02-数据类型转换_自动转换的举例和顺序
This commit is contained in:
25
day02/src/com/inmind/datatype/Demo02.java
Normal file
25
day02/src/com/inmind/datatype/Demo02.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user