JavaSE---基本数据类型&包装类型

 

 

Byte 

 

Byte a = 'a';

0 bipush 97
2 invokestatic #2 <java/lang/Byte.valueOf>
5 astore_1
6 return


Byte a = new Byte((byte) 'a');

 0 new #2 <java/lang/Byte>
 3 dup
 4 bipush 97
 6 invokespecial #3 <java/lang/Byte.<init>>
 9 astore_1
10 return

  

Short

public final class Short extends Number implements Comparable<java.lang.Short> {
        private final short value;
        public Short(short value) {
            this.value = value;
        }

        public boolean equals(Object obj) {
            if (obj instanceof java.lang.Short) {
                return value == ((java.lang.Short)obj).shortValue();
            }
            return false;
        }

        public int hashCode() {
            return java.lang.Short.hashCode(value);
        }

        public static int hashCode(short value) {
            return (int)value;
        }

        public String toString() {
            return Integer.toString((int)value);
        }

        public static java.lang.Short valueOf(short s) {
            final int offset = 128;
            int sAsInt = s;
            if (sAsInt >= -128 && sAsInt <= 127) { // must cache
                return java.lang.Short.ShortCache.cache[sAsInt + offset];
            }
            return new java.lang.Short(s);
        }
    }

  

 

Short c = 12;

0 bipush 12
2 invokestatic #2 <java/lang/Short.valueOf>
5 astore_1
6 return


Short c = new Short((short) 21);

 0 new #2 <java/lang/Short>
 3 dup
 4 bipush 21
 6 invokespecial #3 <java/lang/Short.<init>>
 9 astore_1
10 return

  

ShortCache

valueOf 在value >= -128 || value <= 127,使用的是cache,使用 == 结果是true;

否则,使用的是new Long,  == 结果是false;

 

Integer

public final class Integer extends Number implements Comparable<java.lang.Integer> {

        // Cache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive) as required by JLS.
        private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final java.lang.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() {}
        }

        private final int value;

        public Integer(int value) {
            this.value = value;
        }

        public boolean equals(Object obj) {
            if (obj instanceof Integer) {
                return value == ((Integer)obj).intValue();
            }
            return false;
        }

        public int hashCode() {
            return Integer.hashCode(value);
        }

        public static int hashCode(int value) {
            return value;
        }

        public String toString() {
            return toString(value);
        }

        public static String toString(int i) {
            if (i == java.lang.Integer.MIN_VALUE)
                return "-2147483648";
            int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
            char[] buf = new char[size];
            getChars(i, size, buf);
            return new String(buf, true);
        }

        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    }

  

Integer aa = 12;

// 字节码
3 bipush 12
5 invokestatic #3 <java/lang/Integer.valueOf>
8 astore_2
9 return

  

Integer aa = new Integer(12);

// 字节码
 0 new #2 <java/lang/Integer>
 3 dup
 4 bipush 12
 6 invokespecial #3 <java/lang/Integer.<init>>
 9 astore_1
10 return

  

IntegerCache

valueOf  在value >= -128 || value <= 127,使用的是cache,使用 == 结果是true;

否则,使用的是new Long,  == 结果是false;

 

Long

public final class Long extends Number implements Comparable<java.lang.Long> {
        
        private final long value;

        public Long(long value) {
            this.value = value;
        }

        public int hashCode() {
            return Long.hashCode(value);
        }

        public static int hashCode(long value) {
            return (int)(value ^ (value >>> 32));
        }

        public boolean equals(Object obj) {
            if (obj instanceof Long) {
                return value == ((Long)obj).longValue();
            }
            return false;
        }

        public String toString() {
            return toString(value);
        }

        public static String toString(long i) {
            if (i == Long.MIN_VALUE)
                return "-9223372036854775808";
            int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
            char[] buf = new char[size];
            getChars(i, size, buf);
            return new String(buf, true);
        }

        public static Long valueOf(long l) {
            final int offset = 128;
            if (l >= -128 && l <= 127) { // will cache
                return LongCache.cache[(int)l + offset];
            }
            return new Long(l);
        }

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

    }

  

 

Long a = 12L;

// 字节码
0 ldc2_w #2 <12>
3 invokestatic #4 <java/lang/Long.valueOf>
6 astore_1
7 return

  

 Long a = new Long(12);

// 字节码
0 new #2 <java/lang/Long>
 3 dup
 4 ldc2_w #3 <12>
 7 invokespecial #5 <java/lang/Long.<init>>
10 astore_1
11 return

  

LongCache

valueOf 在value >= -128 || value <= 127,使用的是cache,使用 == 结果是true;

否则,使用的是new Long,  == 结果是false;

 

Double

public final class Double extends Number implements Comparable<java.lang.Double> {
        private final double value;
        public Double(double value) {
            this.value = value;
        }

        public int hashCode() {
            return Double.hashCode(value);
        }

        public static int hashCode(double value) {
            long bits = doubleToLongBits(value);
            return (int)(bits ^ (bits >>> 32));
        }

        public String toString() {
            return toString(value);
        }

        public static String toString(double d) {
            return FloatingDecimal.toJavaFormatString(d);
        }

        public static java.lang.Double valueOf(double d) {
            return new java.lang.Double(d);
        }
    }

  

 

Double a = 128.00;

// 字节码
0 ldc2_w #2 <128.0>
3 invokestatic #4 <java/lang/Double.valueOf>
6 astore_1
7 return

  

Double a = new Double(12.00);

// 字节码
 0 new #2 <java/lang/Double>
 3 dup
 4 ldc2_w #3 <12.0>
 7 invokespecial #5 <java/lang/Double.<init>>
10 astore_1
11 return

  

Float

public final class Float extends Number implements Comparable<java.lang.Float> {

        private final float value;
        public Float(float value) {
            this.value = value;
        }

        public boolean equals(Object obj) {
            return (obj instanceof java.lang.Float)
                    && (floatToIntBits(((java.lang.Float)obj).value) == floatToIntBits(value));
        }

        public int hashCode() {
            return java.lang.Float.hashCode(value);
        }

        public static int hashCode(float value) {
            return floatToIntBits(value);
        }

        public String toString() {
            return java.lang.Float.toString(value);
        }

        public static java.lang.Float valueOf(float f) {
            return new java.lang.Float(f);
        }
        
    }

  

 

Float b = 128.0f;

0 ldc #2 <128.0>
2 invokestatic #3 <java/lang/Float.valueOf>
5 astore_1
6 return




Float b = new Float(128.0);

 0 new #2 <java/lang/Float>
 3 dup
 4 ldc2_w #3 <128.0>
 7 invokespecial #5 <java/lang/Float.<init>>
10 astore_1
11 return

  

Character

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

        private static class CharacterCache {
            private CharacterCache(){}

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

            static {
                for (int i = 0; i < cache.length; i++)
                    cache[i] = new java.lang.Character((char)i);
            }
        }
        
        private final char value;
        public Character(char value) {
            this.value = value;
        }

        public boolean equals(Object obj) {
            if (obj instanceof java.lang.Character) {
                return value == ((java.lang.Character)obj).charValue();
            }
            return false;
        }

        public int hashCode() {
            return java.lang.Character.hashCode(value);
        }

        public static int hashCode(char value) {
            return (int)value;
        }

        public String toString() {
            char buf[] = {value};
            return String.valueOf(buf);
        }

        public static java.lang.Character valueOf(char c) {
            if (c <= 127) { // must cache
                return java.lang.Character.CharacterCache.cache[(int)c];
            }
            return new java.lang.Character(c);
        }
    }

  

 

Character a = 'a';

0 bipush 97
2 invokestatic #2 <java/lang/Character.valueOf>
5 astore_1
6 return


Character a = new Character('a');

 0 new #2 <java/lang/Character>
 3 dup
 4 bipush 97
 6 invokespecial #3 <java/lang/Character.<init>>
 9 astore_1
10 return

  

 

CharacterCache

valueOf 在value <= 127时,取Cache数据,== 结果是true;

否则,== 是false;

 

Boolean

public final class Boolean implements java.io.Serializable, Comparable<java.lang.Boolean>{
        public static final java.lang.Boolean TRUE = new java.lang.Boolean(true);
        public static final java.lang.Boolean FALSE = new java.lang.Boolean(false);
        private final boolean value;
        public Boolean(boolean value) {
            this.value = value;
        }

        public boolean equals(Object obj) {
            if (obj instanceof java.lang.Boolean) {
                return value == ((java.lang.Boolean)obj).booleanValue();
            }
            return false;
        }

        public int hashCode() {
            return java.lang.Boolean.hashCode(value);
        }

        public static int hashCode(boolean value) {
            return value ? 1231 : 1237;
        }

        public String toString() {
            return value ? "true" : "false";
        }

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

  

 

Boolean a = false;

0 iconst_0
1 invokestatic #2 <java/lang/Boolean.valueOf>
4 astore_1
5 return


Boolean a = new Boolean(false);

0 new #2 <java/lang/Boolean>
3 dup
4 iconst_0
5 invokespecial #3 <java/lang/Boolean.<init>>
8 astore_1
9 return

  


  

posted on 2019-02-15 13:59  anpeiyong  阅读(127)  评论(0编辑  收藏  举报

导航