Object类型编译基本类型


byte&short&int

Object a = 1;

// 字节码
0 iconst_1
1 invokestatic #2 <java/lang/Integer.valueOf>
4 astore_1
5 return

  

Object a = 127;

// 字节码
0 bipush 127
2 invokestatic #2 <java/lang/Integer.valueOf>
5 astore_1
6 return

  

Object a = 128;

// 字节码
0 sipush 128
3 invokestatic #2 <java/lang/Integer.valueOf>
6 astore_1
7 return

  

Object a = 2147483647;

// 字节码
0 ldc #2 <2147483647>
2 invokestatic #3 <java/lang/Integer.valueOf>
5 astore_1
6 return

  

Integer.valueOf()

  

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

  

long

 

Object a = 21474836478L;

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

  

Long.valueOf()

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

  

float&double

Object a = 3.4;

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

  

Object a = 3.423;

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

  

Double.valueOf

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

  

char

Object a = 'f';

// 字节码
0 bipush 102
2 invokestatic #2 <java/lang/Character.valueOf>
5 astore_1
6 return

  

Character.valueOf

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

  

boolean

Object a = false;

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

  

Boolean.valueOf

public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }


public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }

  

  

posted on 2024-08-28 17:29  anpeiyong  阅读(2)  评论(0编辑  收藏  举报

导航