原来你是这样的JAVA--[07]聊聊Integer和BigDecimal

打印 上一主题 下一主题

主题 887|帖子 887|积分 2661

今天来聊聊Java中跟数值处理相关的两个类型Integer和BigDecimal。 说起这两个类型,我们肯定都不陌生,但是其中有些容易踩到的坑需要注意避让。
Integer

整型我们应该每天都会用到,但是每种语言还是有自己的特性。从敬姐刚从.NET转过来的时候踩过的一个坑说起:话说在.NET世界中,数值的基本类型和包装类型是会自动转换的,所以数值比较很自然地就会使用 a==b,但是到java这却行不通了,顿时一脸懵。
数值比较及自动装箱
  1.     @Test
  2.     public void Interger(){
  3.         Integer x = 127;
  4.         Integer y = 127;
  5.         Integer m = 99999;
  6.         Integer n = 99999;
  7.         System.out.println("x == y: " + (x==y));
  8.         System.out.println("m == n: " + (m==n));
  9.         System.out.println("x.equals(y): " + x.equals(y));
  10.         System.out.println("m.equals(n): " + m.equals(n));
  11.         //执行结果
  12. //        x == y: true
  13. //        m == n: false
  14. //        x.equals(y): true
  15. //        m.equals(n): true
  16.     }
复制代码
仔细观察可以发现,==比较,较小的两个相同的Integer返回true,较大的两个相同的Integer返回false,这是为何呢?
一起看一下Integer类的源码,发现其中的IntegerCache类。这是Java为了节省空间、提升性能采取的优化机制,常量池的大小为一个字节(-128~127)。
  1. private static class IntegerCache {
  2.         static final int low = -128;
  3.         static final int high;
  4.         static final Integer[] cache;
  5.         static Integer[] archivedCache;
  6.         static {
  7.             // high value may be configured by property
  8.             int h = 127;
  9.             String integerCacheHighPropValue =
  10.                 VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  11.             if (integerCacheHighPropValue != null) {
  12.                 try {
  13.                     h = Math.max(parseInt(integerCacheHighPropValue), 127);
  14.                     // Maximum array size is Integer.MAX_VALUE
  15.                     h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
  16.                 } catch( NumberFormatException nfe) {
  17.                     // If the property cannot be parsed into an int, ignore it.
  18.                 }
  19.             }
  20.             high = h;
  21.             // Load IntegerCache.archivedCache from archive, if possible
  22.             CDS.initializeFromArchive(IntegerCache.class);
  23.             int size = (high - low) + 1;
  24.             // Use the archived cache if it exists and is large enough
  25.             if (archivedCache == null || size > archivedCache.length) {
  26.                 Integer[] c = new Integer[size];
  27.                 int j = low;
  28.                 for(int i = 0; i < c.length; i++) {
  29.                     c[i] = new Integer(j++);
  30.                 }
  31.                 archivedCache = c;
  32.             }
  33.             cache = archivedCache;
  34.             // range [-128, 127] must be interned (JLS7 5.1.7)
  35.             assert IntegerCache.high >= 127;
  36.         }
  37.         private IntegerCache() {}
  38.     }
复制代码
而对于valueOf(int i)方法,直接使用了常量池IntegerCache
[code]  public static Integer valueOf(int i) {        if (i >= IntegerCache.low && i
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表