java中的自动转换概念介绍
This commit is contained in:
23
day02/src/com/inmind/Demo01.java
Normal file
23
day02/src/com/inmind/Demo01.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.inmind;
|
||||
/*
|
||||
自动转换:将取值范围小的类型自动提升为取值范围大的类型
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
//定义2个int变量(4个字节)
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
|
||||
//2个整数相加
|
||||
int sum = a+b;
|
||||
System.out.println(sum);//30
|
||||
|
||||
//定义一个字节型变量(1个字节)
|
||||
byte b1 = 2;
|
||||
int sum1 = a+b1;//先将1字节的b1,自动提升(转换)为4字节的int型,再进行计算的
|
||||
System.out.println(sum1);//12
|
||||
|
||||
//12的整数是可以被一个byte所表示
|
||||
//byte b2 = a+b1;//错误,a+b1发生了自动类型转换,计算之后是一个4字节的int型的值,是不能直接赋值给1字节的类型
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user