zl程序教程

您现在的位置是:首页 >  Java

当前栏目

Java中基本数据类型所占字节大小及取值范围

2023-03-20 14:57:09 时间

一、Java基本数据类型

基本数据类型有种:byte、short、int、long、float、double、boolean、char
分为4类:整数型、浮点型、布尔型、字符型。
整数型:byte、short、int、long
浮点型:float.、double
布尔型:boolean
字符型:char

二、各数据类型所占字节大小

计算机的基本单位:bit.一个bit代表一个0或1
byte:1byte = 8bit,1个字节是8个bit
short:2byte
int:4byte
long:8byte
float:4byte
double:8byte
boolean:1byte
char:2byte

三、各个类型取值范围

1、byte(字节型):[-128 ~ 127],即[-2^7 ~ 2^7-1]

2、short(短整型):[-32768 ~ 32767],即[-2^15 ~ 2^15-1]

3、int(整形):[-2147483648 ~ 2147483647],即[-2^31 ~ 2^31-1]

4、char(字符型):[0 ~ 65535],即[0 ~ 2^16-1]

5、long(长整型):[-2^63 ~ 2^63-1]

6、boolean(布尔型):true、flase

7、double(双精度浮点型):
指数位:-1023 ~ 1024,真正范围为:-1022 ~ 1023
尾数位:52位,2-522-52 = 2.220446049250313E-16,最小是16位,但最小不是1.0E-16,所以精度是15 ~ 16,能保证15,一般16位。

8、float(单精度浮点型):
指数位:127 ~ 128,真正范围为:-126 ~ 127
尾数位:float的尾数:23位,其范围为:0 ~ 223223,而223 = 8388608 = 106.92223 = 8388608 = 106.92,所以float的精度为6 ~ 7位,能保证6位为绝对精确,7位一般也是正确的,8位就不一定了