java中byte、short、char、int的转换

来源:互联网 发布:身边的科学知作文250字 编辑:程序博客网 时间:2024/05/21 09:18

java中byte、short、 char和Int“字面值”之间可以不加强制类型转换,只要int类型的“字面值”不超过byte、short、char类型的范围。如:

byte i = 10(java中默认为int类型);可以转换;byte i = 128;不可以转换,128超出byte类型所能表示的范围,而如果int a = 10; byte b = a;也是不允许的;short、char类似。long和int如果要将long类型转换为int类型则必须要强制类型转换。如:int i = (int)1L;

byte、short、char之间计算不会互相转换,首先转换成int再计算。

boolean类型不能转换成其他类型。

容量小的类型会默认转换为容量大的类型:byte->short->int->long->float->double

将容量大的类型转换成容量小的类型需要进行强制转换。

原创粉丝点击