您现在的位置是:首页 > 文章详情

我看JAVA 之 基本数据类型与封装类型

日期:2019-12-23点击:458

我看JAVA 之 基本数据类型与封装类型

注:基于jdk11
java提供了8中基本数据类型,其中1个布尔类型,6个数字类型,1个字符类型。同时jdk为这8种基本数据类型提供了相应的封装类型。

boolean & Boolean

  • boolean

    1. 长度为1位
    2. 数据范围:只有两个值true、false
    3. 默认值为false
  • Boolean

    1. boolean的封装类型
    2. 实现了Serializable、Comparable接口
    3. 重要的成员变量

      public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); private final boolean value; public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
    4. 重要的方法

      @HotSpotIntrinsicCandidate public boolean booleanValue() { return value; } @HotSpotIntrinsicCandidate public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); } 

byte & Byte

  • byte

    1. 长度为1个字节,有符号
    2. 数据范围:最小值-2^7,最大值2^7-1 即[-128, 127]共256个数字
    3. 默认值为0
  • Byte

    1. 继承了Number类, Number抽象类定义了类型转换的抽象方法如intValue()longValue()floatValue()等,并实现了序列化接口
    2. 实现了Comparable接口
    3. 重要的成员变量

      public static final byte MIN_VALUE = -128; public static final byte MAX_VALUE = 127; @SuppressWarnings("unchecked") public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
    4. 重要的方法

      @HotSpotIntrinsicCandidate public static Byte valueOf(byte b) { final int offset = 128; return ByteCache.cache[(int)b + offset]; } @HotSpotIntrinsicCandidate public byte byteValue() { return value; }
    5. 缓存机制

      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)); } }

      静态内部类ByteCache内部的静态Byte[]缓存了Byte的全部256个数据。

int & Integer

  • int

    1. 长度为4个字节,有符号
    2. 数据范围:最小值-2^31,最大值2^31-1 即[0x80000000, 0x7fffffff]
    3. 默认值为0
  • Integer

    1. 继承了Number类
    2. 实现了Comparable接口
    3. 重要的成员变量

      @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 {@code Class} instance representing the primitive type * {@code int}. * * @since 1.1 */ @SuppressWarnings("unchecked") public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); /** * All possible chars for representing a number as a String */ static final char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }; @Native public static final int SIZE = 32; 32位 public static final int BYTES = SIZE / Byte.SIZE; 32/8=4 4个字节
    4. 重要的方法

      @HotSpotIntrinsicCandidate public static String toString(int i) { int size = stringSize(i); if (COMPACT_STRINGS) { byte[] buf = new byte[size]; getChars(i, size, buf); return new String(buf, LATIN1); } else { byte[] buf = new byte[size * 2]; StringUTF16.getChars(i, size, buf); return new String(buf, UTF16); } } //使用缓存IntegerCache,使用valueOf在空间和时间上要优于直接使用构造方法 @HotSpotIntrinsicCandidate public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } @HotSpotIntrinsicCandidate public int intValue() { return value; }
    5. 有意思的方法:

      System.out.println(Integer.numberOfLeadingZeros(18)); System.out.println(Integer.numberOfTrailingZeros(1000)); System.out.println(Integer.bitCount(1)); System.out.println(Integer.bitCount(2)); System.out.println(Integer.bitCount(4)); System.out.println(Integer.bitCount(12)); 打印结果如下: 27 整数18在二进制表示中首部有27个连续的0 3 整数1000在二进制表示中尾部有3个连续的0 1 整数1在二进制表示中有1位是1 1 整数2在二进制表示中有1位是1 1 整数4在二进制表示中有1位是1 2 整数12在二进制表示中有2位是1 
    6. 缓存机制

      private static class IntegerCache { static final int low = -128; static final int high; //通过设置-XX:AutoBoxCacheMax=<size>,指定high的值,默认缓存范围为[-128,127] static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } 

      验证缓存代码示例:

      package chapter01; public class TestInteger { public static void main(String [] args) { Integer a = Integer.valueOf(6); Integer b = new Integer(6); Integer c = 6; System.out.println(a==b); System.out.println(a==c); System.out.println(c==b); Integer a1 = Integer.valueOf(600); Integer b1 = new Integer(600); Integer c1 = 600; System.out.println("\n"); System.out.println(a1==b1); System.out.println(a1==c1); System.out.println(c1==b1); } } 打印结果: false true false false false false 建议: 包装类型比较是否相等使用equals()而非==

long & Long

  • long

    1. 长度为8个字节,有符号
    2. 数据范围:最小值-2^63,最大值2^63-1 即[0x8000000000000000L, 0x7fffffffffffffffL]
    3. 默认值为0
  • Long

     类似 integer,读者可以自己去分析

float & Float

  • float

    1. 是单精度、长度为4个字节共32位、符合IEEE 754标准的浮点数
    2. 默认值为0.0f
    3. float与double精度不同,所以在将float与double强制转换的时候会出现精度丢失的问题
  • Float

    1. 继承了Number类, Number抽象类定义了类型转换的抽象方法如intValue()longValue()floatValue()等,并实现了序列化接口
    2. 实现了Comparable接口
    3. 重要的成员变量
    public static final float POSITIVE_INFINITY = 1.0f / 0.0f; 正无穷 public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;负无穷 public static final float NaN = 0.0f / 0.0f; //不是数字 Not a Number public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f //最大、最小指数 public static final int MAX_EXPONENT = 127; public static final int MIN_EXPONENT = -126; //32位,4个字节 public static final int SIZE = 32; public static final int BYTES = SIZE / Byte.SIZE; @SuppressWarnings("unchecked") public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");

double & Double

  • double

    1. 是双精度、长度为8个字节共64位、符合IEEE 754标准的浮点数
    2. 默认值为0.0d
  • Double

    类似 Float,读者可以自己去分析 

short & Short

  • short

    1. 长度为2个字节,有符号
    2. 数据范围:最小值-2^15,最大值2^15-1 即[-32768, 32767]
    3. 默认值为0
  • Short

    类似 Integer,读者可以自己去分析

char & Character

  • char

    1. 单个unicode字符,长度为16位
    2. 数据范围:最小值0,最大值65535 即[0, 65535]
    3. 默认值为""
  • Character
原文链接:https://yq.aliyun.com/articles/740515
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章