Java基本数据类型存储与包装类源码解析

19 篇文章 0 订阅
10 篇文章 0 订阅

1. 首先,字节即byte,是二进制,最小的存储单位;位即bit,比特流,常用于网络传输:1byte=8bit。 
 

2. 8种基本数据类型为: byte、short、int、long、float、double、char、boolean

Java中没有类似C语言中的sizeof()函数,该函数的功能是计算变量的字节数。

但是Java中有包装类定义了8种基本数据类型的size(单位bit,比特)。

包装类定义都是final class,final value,意味着创建的包装类对象,不能被继承,栈引用对象一旦创建不能改变值,只能对栈引用新赋值引用

下面将逐一解析

2.1 Byte byte

看Byte源码

public final class Byte extends Number implements Comparable<Byte> {
    /**
     * The value of the {@code Byte}.
     *
     * @serial
     */
    private final byte value;

    /**
     * A constant holding the minimum value a {@code byte} can
     * have, -2<sup>7</sup>.
     */
    public static final byte   MIN_VALUE = -128;

    /**
     * A constant holding the maximum value a {@code byte} can
     * have, 2<sup>7</sup>-1.
     */
    public static final byte   MAX_VALUE = 127;


    /**
     * The number of bits used to represent a {@code byte} value in two's
     * complement binary form.
     *
     * @since 1.5
     */
    public static final int SIZE = 8;

    /**
     * The number of bytes used to represent a {@code byte} value in two's
     * complement binary form.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

源码定义Byte的本质final byte value,byte的范围-128~127,-2的7次方到2的7次方,每个byte为8个bits,byte是最小存储单位,源码看出byte自身单位1,SIZE/Byte.SIZE必定等于1

2.2 Short short

public final class Short extends Number implements Comparable<Short> {
    /**
     * The value of the {@code Short}.
     *
     * @serial
     */
    private final short value;

    /**
     * A constant holding the minimum value a {@code short} can
     * have, -2<sup>15</sup>.
     */
    public static final short   MIN_VALUE = -32768;

    /**
     * A constant holding the maximum value a {@code short} can
     * have, 2<sup>15</sup>-1.
     */
    public static final short   MAX_VALUE = 32767;

    /**
     * The number of bits used to represent a {@code short} value in two's
     * complement binary form.
     * @since 1.5
     */
    public static final int SIZE = 16;

    /**
     * The number of bytes used to represent a {@code short} value in two's
     * complement binary form.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

源码定义Short的本质final short value,short的范围-32768~32767,-2的15次方到2的15次方,每个short为16个bits,byte是最小存储单位,源码看出short的byte单位2,SIZE/(Byte.SIZE = 8)必定等于2

2.3 Integer int

public final class Integer extends Number implements Comparable<Integer> {
    /**
     * The value of the {@code Integer}.
     *
     * @serial
     */
    private final int value;

    /**
     * A constant holding the minimum value an {@code int} can
     * have, -2<sup>31</sup>.
     */
    @Native public static final int   MIN_VALUE = 0x80000000;

    /**
     * A constant holding the maximum value an {@code int} can
     * have, 2<sup>31</sup>-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;

    /**
     * The number of bits used to represent an {@code int} value in two's
     * complement binary form.
     *
     * @since 1.5
     */
    @Native public static final int SIZE = 32;

    /**
     * The number of bytes used to represent a {@code int} value in two's
     * complement binary form.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

源码定义Integer的本质final int value,int的范围16进制表示,-2的31次方到2的31次方,每个int为32个bits,byte是最小存储单位,源码看出int的byte单位4,SIZE/(Byte.SIZE = 8)必定等于4。 

2.4 Long long

public final class Long extends Number implements Comparable<Long> {
    /**
     * The value of the {@code Long}.
     *
     * @serial
     */
    private final long value;

    /**
     * A constant holding the minimum value a {@code long} can
     * have, -2<sup>63</sup>.
     */
    @Native public static final long MIN_VALUE = 0x8000000000000000L;

    /**
     * A constant holding the maximum value a {@code long} can
     * have, 2<sup>63</sup>-1.
     */
    @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

    /**
     * The number of bits used to represent a {@code long} value in two's
     * complement binary form.
     *
     * @since 1.5
     */
    @Native public static final int SIZE = 64;

    /**
     * The number of bytes used to represent a {@code long} value in two's
     * complement binary form.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

源码定义Long的本质final long value,long的范围16进制表示,-2的63次方到2的63次方,每个long为64个bits,byte是最小存储单位,源码看出long的byte单位8,SIZE/(Byte.SIZE = 8)必定等于8。  

2.5 Float float

public final class Float extends Number implements Comparable<Float> {
    /**
     * The value of the Float.
     *
     * @serial
     */
    private final float value;

    /**
     * The number of bits used to represent a {@code float} value.
     *
     * @since 1.5
     */
    public static final int SIZE = 32;

    /**
     * The number of bytes used to represent a {@code float} value.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

   
    /**
     * A constant holding the largest positive finite value of type
     * {@code float}, (2-2<sup>-23</sup>)&middot;2<sup>127</sup>.
     * It is equal to the hexadecimal floating-point literal
     * {@code 0x1.fffffeP+127f} and also equal to
     * {@code Float.intBitsToFloat(0x7f7fffff)}.
     */
    public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f

    /**
     * A constant holding the smallest positive nonzero value of type
     * {@code float}, 2<sup>-149</sup>. It is equal to the
     * hexadecimal floating-point literal {@code 0x0.000002P-126f}
     * and also equal to {@code Float.intBitsToFloat(0x1)}.
     */
    public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f


    

 源码定义Float的本质final float value,float的范围16进制表示, 0x0.000002P-126f~0x1.fffffeP+127f,每个float为32个bits,byte是最小存储单位,源码看出float的byte单位4,SIZE/(Byte.SIZE = 8)必定等于4。  

2.6 Double double

public final class Double extends Number implements Comparable<Double> {

    /**
     * The value of the Double.
     *
     * @serial
     */
    private final double value;

    
    /**
     * A constant holding the largest positive finite value of type
     * {@code double},
     * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
     * the hexadecimal floating-point literal
     * {@code 0x1.fffffffffffffP+1023} and also equal to
     * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
     */
    public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308

   
    /**
     * A constant holding the smallest positive nonzero value of type
     * {@code double}, 2<sup>-1074</sup>. It is equal to the
     * hexadecimal floating-point literal
     * {@code 0x0.0000000000001P-1022} and also equal to
     * {@code Double.longBitsToDouble(0x1L)}.
     */
    public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324


    /**
     * The number of bits used to represent a {@code double} value.
     *
     * @since 1.5
     */
    public static final int SIZE = 64;

    /**
     * The number of bytes used to represent a {@code double} value.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

 源码定义Double的本质final double value,double的范围16进制表示, 0x0.0000000000001P-1022~0x1.fffffffffffffP+1023,每个double为64个bits,byte是最小存储单位,源码看出float的byte单位8,SIZE/(Byte.SIZE = 8)必定等于8。  

2.7 Character char

public final
class Character implements java.io.Serializable, Comparable<Character> {
    /**
     * The value of the {@code Character}.
     *
     * @serial
     */
    private final char value;

    
    /**
     * The constant value of this field is the smallest value of type
     * {@code char}, {@code '\u005Cu0000'}.
     *
     * @since   1.0.2
     */
    public static final char MIN_VALUE = '\u0000';

    /**
     * The constant value of this field is the largest value of type
     * {@code char}, {@code '\u005CuFFFF'}.
     *
     * @since   1.0.2
     */
    public static final char MAX_VALUE = '\uFFFF';

    /**
     * The number of bits used to represent a <tt>char</tt> value in unsigned
     * binary form, constant {@code 16}.
     *
     * @since 1.5
     */
    public static final int SIZE = 16;

    /**
     * The number of bytes used to represent a {@code char} value in unsigned
     * binary form.
     *
     * @since 1.8
     */
    public static final int BYTES = SIZE / Byte.SIZE;

源码定义Character的本质final char value,Character范围unicode示, 每个char为16个bits,byte是最小存储单位,源码看出char的byte单位2,SIZE/(Byte.SIZE = 8)必定等于2。   

2.8 Boolean boolean

public final class Boolean implements java.io.Serializable,
                                      Comparable<Boolean>
{

    /**
     * The value of the Boolean.
     *
     * @serial
     */
    private final boolean value;

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);

源码定义Boolean的本质final boolean value,没有定义SIZE,但是boolean很简单,一个byte完全可以存储,甚至一个bit都可以存储。

3. String

String的源码

JDK1.8 char数组

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

JDK1.9

private final byte[] value;

变成了byte数组空间利用率更高。赞,下一遍专门写一遍JDK1.9的String源码分析 

 下面来测试JDK1.8,JDK1.9,JDK1.10

        String c = "我";

        System.out.println("gbk:" + c.getBytes("gbk").length);
        System.out.println("utf-8:" + c.getBytes("utf-8").length);
        System.out.println("gb2312:" + c.getBytes("gb2312").length);

        结果
        gbk:2
        utf-8:3
        gb2312:2

可以看出String的char数组存储的char元素字节大小跟编码格式有关 。

4. 总结

以byte为单位1,各种基本数据类型内存占用的byte。

byte        1    
short       2    
int           4     
long        8    
float        4    
double    8    
char        2    
boolean  1

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值