java基本数据类型与包装类间的转换

基本数据类型之间的转换规则

  1. 在一个双操做数以及位运算等算术运算式中,会根据操做数的类型将低级的数据类型自动转换为高级的数据类型(向低转有可能出现值范围溢出的异常)分为如下几种状况:
    (优先级 double > float > long > int > (byte = short = char) )
    1. 只要两个操做数中有一个是double类型的,另外一个将会被转换成double类型,而且结果也是double类型;
    2. 只要两个操做数中有一个是float类型的,另外一个将会被转换成float类型,而且结果也是float类型;
    3. 只要两个操做数中有一个是long类型的,另外一个将会被转换成long类型,而且结果也是long类型;
    4. 两个操做数(包括byte、short、int、char)都将会被转换成int类型,而且结果也是int类型。
  2.  若是低级类型为char型,向高级类型(整型)转换时,会转换为对应ASCII码值,再作其它类型的自动转换。
  3. 对于byte,short,char三种类型而言,他们是平级的,所以不能相互自动转换,可使用下述的强制类型转换。 eg:short i=99 ; char c=(char)i; System.out.println("output:"+c)
  4. 不能在布尔值和任何数字类型间强制类型转换;
  5.  不一样级别数据类型间的强制转换,可能会致使溢出或精度的降低。
  6. 当字节类型变量参与运算,java做自动数据运算类型的提高,将其转换为int类型。eg:byte b;  b=3;  b=(byte)(b*3);//必须声明byte

装箱&拆箱

装箱都是执行valueOf方法:若是有缓存将断定是否在缓存范围内,不然new。html

拆箱则是执行xxxValue方法!<floatValue、longValue、intValue。。。>java

eg.数组

public static boolean isSuspend(Double code) {
        return code == (int) 1;
    }

    public static void main(String[] args) {
        isSuspend(2d);
    }

入参先作装箱处理Double.valueOf;再作拆箱处理Double.doubleValue。
说明:拆箱与当前本身的包装类有关!缓存

Java 语言规范中的缓存行为

在 Boxing Conversion 部分的Java语言规范(JLS)规定以下:oracle

详情可见: https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7 下的 【5.1.7. Boxing Conversion】、【5.1.8. Unboxing Conversion】app

if the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
若是一个变量 p 的值属于:-128至127之间的整数,true 和 false的布尔值,’u0000′ 至 ‘u007f’ 之间的字符中时,将 p 包装成 a 和 b 两个对象时,能够直接使用 a == b 判断 a 和 b 的值是否相等。ide

A boxing conversion may result in an OutOfMemoryError if a new instance of one of the wrapper classes (BooleanByteCharacterShortIntegerLongFloat, or Double) needs to be allocated and insufficient storage is available.性能

5.1.7 章节阐述: ui

  1. 当内存空间不足以分配时,出现OutOfMemoryError; 
  2. 对于除 float、double外的的基本数据类型在自动装箱时有缓存Cache; 
  3. 在float、double装箱时,须要关注是不是NaN(Not-a-Number)

5.1.8 章节在关于拆箱的描述中: 若是为null时,将抛出NullPointerException.this

Double:

    /**
     * A constant holding a Not-a-Number (NaN) value of type
     * {@code double}. It is equivalent to the value returned by
     * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
     */
    public static final double NaN = 0.0d / 0.0;


Float:

   /**
     * A constant holding a Not-a-Number (NaN) value of type
     * {@code float}.  It is equivalent to the value returned by
     * {@code Float.intBitsToFloat(0x7fc00000)}.
     */
    public static final float NaN = 0.0f / 0.0f;

eg.

Integer aa = 127, bb = 127;
        System.out.println(aa == bb); //true  
        aa = 128;
        bb = 128;
        System.out.println(aa == bb); //false  --由于128超出范围
        System.out.println(aa.equals(bb)); //true  

        Integer a = 10; //this is autoboxing
        Integer b = Integer.valueOf(10); //under the hood
        System.out.println(a == b); // true

        Float c = 0f / 0f;
        Float d = 0f;

        System.out.println(c == d); // false -- c是NaN
        System.out.println(c.isNaN()); // true
        System.out.println(d.isNaN()); // false

        Double e = 0d / 0d;
        Double f = 0d;

        System.out.println(e == f); // false -- e是NaN
        System.out.println(e.isNaN()); // true
        System.out.println(f.isNaN()); // false

Boolean、Byte、Short、Integer、Long、Character 的缓存机制

Integer

在 Java 5 中,为 Integer 的操做引入了一个新的特性,用来节省内存和提升性能。整型对象在内部实现中经过使用相同的对象引用实现了缓存和重用。上面的规则适用于整数区间 -128 到 +127
这种 Integer 缓存策略仅在自动装箱(autoboxing)的时候有用,使用构造器建立的 Integer 对象不能被缓存。
Java 编译器把原始类型自动转换为封装类的过程称为自动装箱(autoboxing),这至关于调用 valueOf 方法!

Integer a = 10; //this is autoboxing
Integer b = Integer.valueOf(10); //under the hood

源码以下:

在-128到127区间将取缓存中的,超出范围后会new新的地址堆对象。

/**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

IntegerCache

 Integer 类中一个私有的静态类。

Javadoc 详细的说明这个类是用来实现缓存支持,并支持 -128 到 127 之间的自动装箱过程。最大值 127 能够经过 JVM 的启动参数 -XX:AutoBoxCacheMax=size 修改。 缓存经过一个 for 循环实现。从小到大的建立尽量多的整数并存储在一个名为 cache 的整数数组中。这个缓存会在 Integer 类第一次被使用的时候被初始化出来。之后,就可使用缓存中包含的实例对象,而不是建立一个新的实例(在自动装箱的状况下)。

实际上在 Java 5 中引入这个特性的时候,范围是固定的 -128 至 +127。后来在 Java 6 中,最大值映射到 java.lang.Integer.IntegerCache.high,可使用 JVM 的启动参数设置最大值。这使咱们能够根据应用程序的实际状况灵活地调整来提升性能。是什么缘由选择这个 -128 到 127 这个范围呢?由于这个范围的整数值是使用最普遍的。 在程序中第一次使用 Integer 的时候也须要必定的额外时间来初始化这个缓存。

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.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() {}
    }

其余缓存

Byte,Short,Long、Integer的缓存有固定范围: -128 到 127,对于 Character缓存范围是 0 到 127除了 Integer 能够经过参数改变范围外,其它的都不行。

tip1: 
    String 的 valueOf方法只有对boolean的处理返回字面量(字符串常量池), 其余都是new新的!

    public static String valueOf(boolean b) {
        return b ? "true" : "false";


    }

tip2: 对于Byte而言值的范围在[-128,127] 

byte占一个字节空间,最高位是符号位,剩余7位能表示0-127,加上符号位的正负,就是-127至+127,但负0不必,为充分利用,就用负零表示-128(即原码1000,0000)。(计算机转补码后存储)

对于boolean类型: 提供静态的2中枚举值

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

ByteCache 缓存 Byte 、ShortCache 缓存 Short 、 LongCache 缓存 Long <三者源码相似,>

private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }

CharacterCache 缓存 Character

private static class CharacterCache {
        private CharacterCache(){}

        static final Character cache[] = new Character[127 + 1];

        static {
            for (int i = 0; i < cache.length; i++)
                cache[i] = new Character((char)i);
        }
    }
 
eg. 在断点单测时发现 都跳入了valueOf方法!
public static void main(String[] args) {
        Long a = 3L;
        Integer b = 4;
        Character c = 5;
        Byte d = 7;
        Short e = 1;
        System.out.println(a == Long.valueOf(a));
        System.out.println(b == Integer.valueOf(b));
        System.out.println(c == Character.valueOf(c));
        System.out.println(d == Byte.valueOf(d));
        System.out.println(e == Short.valueOf(e));
    }

运行的结果都为true!

相关文章
相关标签/搜索