八个基本数据类型及长度
8种基本数据类型为: 4种整形:byte,short),int,long 2种浮点类型:float,double 1种Unicode编码的字符单元的字符型:char 1中Boolean类型:boolean
8中类型所占字节和位数和取值范围如下:
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不能够互用,因为他们两种不同的数据类型;
上一篇:
IDEA上Java项目控制台中文乱码