• Integeter127与128


     1 package com.hangao.basic;
     2 
     3 /**
     4  * @author hangao hangao1204@hotmail.com
     5  *
     6  */
     7 public class TestInt {
     8     public static void main(String[] args) {
     9         Integer ie1 = 127;
    10         Integer ie2 = 127;
    11         System.out.println("Integer 127==127:" + (ie1 == ie2));//Integer 127==127:true
    12         
    13         Integer ie3 = new Integer(127);
    14         Integer ie4 = new Integer(127);
    15         System.out.println("new Integer 127==127:" + (ie3 == ie4));//new Integer 127==127:false
    16         
    17         Integer ie5 = 128;
    18         Integer ie6 = 128;
    19         System.out.println("Integer 128==128:" + (ie5 == ie6));//Integer 128==128:false
    20         
    21         Integer ie7 = new Integer(128);
    22         Integer ie8 = new Integer(128);
    23         System.out.println("new Integer 128==128:" + (ie7 == ie8));//new Integer 128==128:false
    24         
    25         analysis();
    26     }
    27 
    28     private static void analysis() {
    29         Integer ie0 = 127;
    30         System.out.println(ie0);
    31         /* L0
    32         LINENUMBER 5 L0
    33         BIPUSH 127
    34         INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
    35         ASTORE 1*/
    36         /*从字节码文件可以看出此处有一个自动装箱的动作*/
    37         
    38         /*public static Integer valueOf(int i) {
    39             if (i >= IntegerCache.low && i <= IntegerCache.high)
    40                 return IntegerCache.cache[i + (-IntegerCache.low)];
    41             return new Integer(i);
    42         }*/
    43         /*而Integeter这个类的装箱过程从Integeter源代码可以看出在(-128 - 127)之间的int值
    44         其实返回的是IntegerCache里面的cache[]数组里面的对象*/
    45         
    46         /*private static class IntegerCache {
    47             static final int low = -128;
    48             static final int high;
    49             static final Integer cache[];
    50 
    51             static {
    52                 // high value may be configured by property
    53                 int h = 127;
    54                 String integerCacheHighPropValue =
    55                     sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    56                 if (integerCacheHighPropValue != null) {
    57                     try {
    58                         int i = Integer.parseInt(integerCacheHighPropValue);
    59                         i = Math.max(i, 127);
    60                         // Maximum array size is Integer.MAX_VALUE
    61                         h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
    62                     } catch( NumberFormatException nfe) {
    63                         // If the property cannot be parsed into an int, ignore it.
    64                     }
    65                 }
    66                 high = h;
    67 
    68                 cache = new Integer[(high - low) + 1];
    69                 int j = low;
    70                 for(int k = 0; k < cache.length; k++)
    71                     cache[k] = new Integer(j++);
    72 
    73                 // range [-128, 127] must be interned (JLS7 5.1.7)
    74                 assert IntegerCache.high >= 127;
    75             }
    76 
    77             private IntegerCache() {}
    78         }*/
    79         /*IntegerCache是Integer的内部类,在被jdk加载的时候,会去初始化创建(-128 - 127)之间所有int值对应的Integer类数组
    80         使用的时候可以从里面取*/
    81     }
    82 }
  • 相关阅读:
    POJ 1795 DNA Laboratory
    CodeForces 303B Rectangle Puzzle II
    HDU 2197 本源串
    HDU 5965 扫雷
    POJ 3099 Go Go Gorelians
    CodeForces 762D Maximum path
    CodeForces 731C Socks
    HDU 1231 最大连续子序列
    HDU 5650 so easy
    大话接口隐私与安全 转载
  • 原文地址:https://www.cnblogs.com/hangao/p/6062241.html
Copyright © 2020-2023  润新知