Java数据类型内存大小与包容性

来源:互联网 发布:linux怎么改名 编辑:程序博客网 时间:2024/06/13 01:51

double - 8 bytes

float - 4 bytes

long - 8 bytes

int - 4 bytes

short - 2 bytes

byte - 1 bytes

数据类型包容性从上往下,由宽到窄。


Java always takes narrower type.

Example: 

int x = 3, y = 4;double z = x / y;System.out.println("z = " + z);

输出为:

z = 0.0;

int x = 3, y = 4;double z = (double) x / y;System.out.println("z = " + z);
输出为:

z = 0.0;

int x = 3, y = 4;double z = (double) x / (double) y;System.out.println("z = " + z);
输出为:

z = 0.75;

想要upcast成wider的数据类型,就得在所有运算过程中的narrow的数据类型前upcast。


原创粉丝点击