Java常量池——Integer

来源:互联网 发布:php array diff keys 编辑:程序博客网 时间:2024/05/20 04:11

原文链接:http://blog.csdn.net/shw2004/article/details/5678703

Java的8种基本类型(Byte, Short, Integer, Long, Character, Boolean, Float, Double), 除Float和Double以外, 其它六种都实现了常量池, 但是它们只在大于等于-128并且小于等于127时才使用常量池。

由如下例子可以看出:

[java] view plain copy
  1. public static void main(String[] args) {  
  2.     Integer a = 127;  
  3.     Integer b = 127;  
  4.     System.out.println("等于127:");  
  5.     System.out.println(a == b);  
  6.     System.out.println("*****************");  
  7.   
  8.     a = 128;  
  9.     b = 128;  
  10.     System.out.println("等于128:");  
  11.     System.out.println(a == b);  
  12.     System.out.println("*****************");  
  13.   
  14.     a = -128;  
  15.     b = -128;  
  16.     System.out.println("等于-128:");  
  17.     System.out.println(a == b);  
  18.     System.out.println("*****************");  
  19.   
  20.     a = -129;  
  21.     b = -129;  
  22.     System.out.println("等于-129:");  
  23.     System.out.println(a == b);  
  24.     System.out.println("*****************");  
  25.   
  26.     // <a href="http://lib.csdn.net/base/softwaretest" class='replace_word' title="软件测试知识库" target='_blank' style='color:#df3434; font-weight:bold;'>测试</a>Boolean  
  27.     System.out.println("测试Boolean");  
  28.     Boolean c = true;  
  29.     Boolean d = true;  
  30.     System.out.println(c == d);  
  31.     d = new Boolean(true);  
  32.     System.out.println(c == d);  
  33. }  

结果如下:

等于127:
true
*****************
等于128:
false
*****************
等于-128:
true
*****************
等于-129:
false
*****************
测试Boolean
true
false

 

当我们给Integer赋值时,实际上调用了Integer.valueOf(int)方法,查看源码,其实现如下:

[java] view plain copy
  1. public static Integer valueOf(int i) {  
  2.     if(i >= -128 && i <= IntegerCache.high)  
  3.         return IntegerCache.cache[i + 128];  
  4.     else  
  5.         return new Integer(i);  
  6. }  

而IntegerCache实现如下:

[java] view plain copy
  1. private static class IntegerCache {  
  2.     static final int high;  
  3.     static final Integer cache[];  
  4.   
  5.     static {  
  6.         final int low = -128;  
  7.   
  8.         // high value may be configured by property  
  9.         int h = 127;  
  10.         if (integerCacheHighPropValue != null) {  
  11.             // Use Long.decode here to avoid invoking methods that  
  12.             // require Integer's autoboxing cache to be initialized  
  13.             int i = Long.decode(integerCacheHighPropValue).intValue();  
  14.             i = Math.max(i, 127);  
  15.             // Maximum array size is Integer.MAX_VALUE  
  16.             h = Math.min(i, Integer.MAX_VALUE - -low);  
  17.         }  
  18.         high = h;  
  19.   
  20.         cache = new Integer[(high - low) + 1];  
  21.         int j = low;  
  22.         for(int k = 0; k < cache.length; k++)  
  23.             cache[k] = new Integer(j++);  
  24.     }  
  25.   
  26.     private IntegerCache() {}  
  27. }  

注意cache数组是静态的。

所以,Integer类型最好不要直接用==比较。