栏目:Java8基础 作者:admin 日期:2015-01-11 评论:1 点击: 1,790 次
本文标签: java.math.BigInteger系列教程
缓存在JDK中使用的非常多,本文给大家说一下BigInteger类的缓存内容。它的缓存包括下面几个部分:
第一:它的内部已经生成了三个常量:
1 2 3 4 |
public static final BigInteger ZERO = new BigInteger(new int[0], 0); public static final BigInteger ONE = valueOf(1); private static final BigInteger TWO = valueOf(2); public static final BigInteger TEN = valueOf(10); |
第二:查看 valueOf()方法的源码,我们还可以发现另外一个地方的缓存实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static BigInteger valueOf(long val) { if (val == 0) { return ZERO; } if (val > 0 && val <= MAX_CONSTANT) { return posConst[(int) val]; } else if (val < 0 && val >= -MAX_CONSTANT) { return negConst[(int) -val]; } return new BigInteger(val); } |
通过上面的源码,我们可以发现posConst[]和negConst[]也是缓存数据的地方:
1 2 3 4 5 6 7 8 9 10 11 12 |
private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT + 1]; private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT + 1]; static { for (int i = 1; i <= MAX_CONSTANT; i++) { int[] magnitude = new int[1]; magnitude[0] = i; posConst[i] = new BigInteger(magnitude, 1); negConst[i] = new BigInteger(magnitude, -1); } } |
------====== 本站公告 ======------
金丝燕网,一个严谨的网站!