数字作为字符占用的空间是否小于数字作为整数的空间?

时间:2018-10-15 11:23:38

标签: java char numbers int

例如,

char x = '1';

占用的空间少于

int x = 1;

还是一样?

4 个答案:

答案 0 :(得分:7)

char占用2个字节,int占用4个字节。

是的,是的。 char占用的空间更少。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

答案 1 :(得分:2)

一个char需要16位。 一个int需要32位。

答案 2 :(得分:0)

Char将字符常量存储在内存中。它假定大小为2 bytes,但是基本上它只能容纳一个字符,因为char存储 unicode 字符集。最小值为‘u0000’(或0),最大值为‘uffff’(或65,535,包括端值)。但是整数为4 bytes

其他原始类型的范围和大小;

Type            Size            Range of values that can be stored
byte            1 byte          −128 to 127
short           2 bytes         −32768 to 32767
int             4 bytes         −2,147,483,648 to 2,147,483,647
long            8 bytes         9,223,372,036,854,775,808 to9,223,372,036,854,755,807
float           4 bytes         3.4e−038 to 3.4e+038
double          8 bytes         1.7e−308 to 1.7e+038

答案 3 :(得分:0)

IntegerCharacter类中,有一个SIZE字段,这表示使用空间(位)。整数使用32位,字符使用16位。