美文网首页
JDK源码解析-Integer

JDK源码解析-Integer

作者: have1568 | 来源:发表于2019-05-26 22:14 被阅读0次

    一个有东西的需求:

    要求在swap()方法里处理后输入a=2,b=1
        public static void main(String[] args) throws NoSuchFieldException {
            Integer a = 1;
            Integer b = 2;
            System.out.println("a = " + a + "\tb = " + b);
            swap(a, b);
            System.out.println("a = " + a + "\tb = " + b);
        }
        private static void swap(Integer a, Integer b) throws NoSuchFieldException {
          //TODO
        }
    

    错误示例1

        private static void swap(Integer a, Integer b) throws NoSuchFieldException {
            Integer c = a;
            a = b;
            b = c;
        }
    
    示例1原因解析

    输出结果:
    a = 1 b = 2
    a = 1 b = 2
    传递的是引用,一般来讲,是可以修改引用对应的对象的值,

    示例2原因解析

    执行查看java字节码问键命令

     javap -c IntegerExample.class
    

    输出结果

    Compiled from "IntegerExample.java"
    public class net.wang.num.IntegerExample {
      public net.wang.num.IntegerExample();
        Code:
           0: aload_0
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return
           
      public static void main(java.lang.String[]) throws java.lang.NoSuchFieldException;
        Code:
           0: iconst_1
           1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
           4: astore_1
           5: iconst_2
           6: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
           9: astore_2
          10: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
          13: new           #4                  // class java/lang/StringBuilder
          16: dup
          17: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
          20: ldc           #6                  // String a =
          22: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          25: aload_1
          26: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
          29: ldc           #9                  // String          b =
          31: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          34: aload_2
          35: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
          38: invokevirtual #10                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
          41: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          44: aload_1
          45: aload_2
          46: invokestatic  #12                 // Method swap:(Ljava/lang/Integer;Ljava/lang/Integer;)V
          49: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
          52: new           #4                  // class java/lang/StringBuilder
          55: dup
          56: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
          59: ldc           #6                  // String a =
          61: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          64: aload_1
          65: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
          68: ldc           #9                  // String          b =
          70: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          73: aload_2
          74: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
          77: invokevirtual #10                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
          80: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          83: return
    }
    

    这一行表示 Integer a = 1;实际上编译后变成Integer a = Integer.valueOf(1);

     1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    

    Integer.valueOf(),属于自动装箱胡知识,源码如下:
    在-128到127之间,直接从数组里取值

        //在-128到127之间,直接从数组里取值
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    

    IntegerCache的源码如下
    初始化IntegerCache内部类,如果没有配置虚拟机参数:java.lang.Integer.IntegerCache.high,则取值-128~127,配置了,取值范围在-128~2^31次方之间

       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() {}
       }
    

    相关文章

      网友评论

          本文标题:JDK源码解析-Integer

          本文链接:https://www.haomeiwen.com/subject/jlqkhqtx.html