向上转换:

整型,字符型,浮点型的数据在混合运算中相互转换,转换时遵循以下原则:

容量小的类型可自动转换为容量大的数据类型;

byte,short,char → int → long → float → double

byte,short,char之间不会相互转换,他们在计算时首先会转换为int类型。

boolean 类型是不可以转换为其他基本数据类型。

Eg:

int i = 123;

long l = i;       //自动转换,不需强转

float f = 3.14F;

double d = f;

 

向下转换:

整型,字符型,浮点型的数据在混合运算中相互转换,转换时遵循以下原则:

容量小的类型可自动转换为容量大的数据类型;

byte,short,char → int → long → float → double

byte,short,char之间不会相互转换,他们在计算时首先会转换为int类型。

boolean 类型是不可以转换为其他基本数据类型。

Eg:

long l = 123L;

int i = (int) l;//必须强转

double d = 3.14;

float f = (float) d;

 

我的总结:类型转化

小转大,自动!自动类型转换(也叫隐式类型转换)

大转小,强转!强制类型转换(也叫显式类型转换)

相关文章:

  • 2021-12-12
  • 2021-11-27
  • 2021-08-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-07-08
  • 2021-04-19
相关资源
相似解决方案