首先我们来简单的了解一下,基础数据类型的数值范围,如下图所示:

Boolean 类型定义如下:
public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
public static final Class<Boolean> TYPE =
(Class<Boolean>)Class.getPrimitiveClass("boolean");
private final boolean value;
public Boolean(boolean value) {
this.value = value;
}
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
}
java的基本数据类型中,boolean只有两种状态,默认值为false.取值范围是{true,false},理论上占1bit,实际上:
- 1.单个的boolean 类型变量在编译的时候是使用的int 类型。
boolean a=true;//这个a在JVM中占4个字节即:32位。
- 2.boolean 类型的数组时,在编译的时候是作为byte array来编译的所以boolean 数组里面的每一个元件占一个字节,
boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。
Byte定义如下:
public final class Byte extends Number implements Comparable<Byte> {
public static final byte MIN_VALUE = -128;
public static final byte MAX_VALUE = 127;
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; I++)
cache[i] = new Byte((byte)(i - 128));
}
}
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
}
在Byte对象中也有一个private final的byte的属性。byte的取值范围为-128-127.在Byte中有一个内部类是ByteCache,用来缓存byte的数据,所以对于byte类型赋值,JVM不会再开辟空间,如果byte类型采用new关键字创建对象JVM会在堆区产生对象,byte可以转换成short,int,long,float,double,toString.其中有parseByte(String),parseByte(String,radix)方法,可以将其他进制的字符串,转换成十进制。
网友评论