八个基本数据类型及长度

8种基本数据类型为: 4种整形:byte,short),int,long 2种浮点类型:float,double 1种Unicode编码的字符单元的字符型:char 1中Boolean类型:boolean

8中类型所占字节和位数和取值范围如下:

类型 占用字节 占用位数 数值长度 byte 1 8 -128~127(-2的7次方到2的7次方-1) short 2 16 -32768~32767(-2的15次方到2的15次方-1) int 4 32 -2147483648~2147483647(-2的31次方到2的31次方-1) long 8 64 (-9223372036854774808~9223372036854774807)(-2的63次方到2的63次方-1) float 4 32 ( 1.401298e-45~3.402823e+38)(e-45是乘以10的负45次方 ,e+38是乘以10的38次方 )(2的-149次方 ~ 2的128次方-1) double 8 64 (4.9000000e-324 ~ 1.797693e+308 )(2的-1074次方 , 2的1024次方-) char 2 16 boolean 1

boolean 所占字节问题

The boolean type is compiled into the int type for use, occupying 4 bytes.
 The boolean array is compiled into a byte array type, and each boolean array member occupies 1 byte.
 In the Java virtual machine, 1 means true and 0 means false.
 This is just a recommendation of the Java Virtual Machine.
 To be sure, it will not be 1 bit.

1 byte = 1字节 = 8bit

二、小插曲 -----int和Integer的区别

1.从定义上来看

int 是基本类型,直接存数值(类似的还有float、double、String、char)

Integer是对象,用一个引用指向这个对象(类似的有Float、Double、String)

2.初始化的方式不同

int i =1;

Integer i= new Integer(1);//integer 是一个类

int 是基本数据类型(面向过程留下的痕迹,不过是对java的有益补充);Integer 是一个类,是int的扩展,定义了很多的转换方法

注意:类似的还有:float Float;double Double;String等,其中String较为特殊,基本类型和复杂类似关键字相同。

例如,当需要往ArrayList,HashMap中放东西时,像int,double这种内建类型是放不进去的,因为容器都是装 object的,这是就需要这些内建类型的外覆类了。Java中每种内建类型都有相应的外覆类。

Java中int和Integer关系是比较微妙的。关系如下:

    int是基本的数据类型; Integer是int的封装类; int和Integer都可以表示某一个数值; int和Integer不能够互用,因为他们两种不同的数据类型;
经验分享 程序员 微信小程序 职场和发展