1

I'm having a bit of trying to figure if the variables used when creating an object persist in Java.

Specifically I'm looking at BigInteger. If I'm reading the code correctly it looks like instead of doing addition etc. on a bit by bit basis the number is broken up into 32bit words which allows for faster operation. What I have not been able to figure out is whether this 32bit word representation and other variables (mag[], signum etc.) have to be created everytime a method is used on a BigInteger or if it somehow they persists in cache and remain associated with their particular BigInteger once it has been created.

4

2 回答 2

2

这些只是普通的对象字段——它们是对象的存储方式。它们不是“每次在 BigInteger 上使用方法时创建的”——它们是从什么创建的?ABigInteger 实现为该组字段;没有其他神奇的实现可以从中推断出来。

于 2013-08-09T20:20:23.087 回答
2

你正在看这段代码:

 1054    public BigInteger add(BigInteger val) {
 1055         int[] resultMag;
 1056         if (val.signum == 0)
 1057             return this;
 1058         if (signum == 0)
 1059             return val;
 1060         if (val.signum == signum)
 1061             return new BigInteger(add(mag, val.mag), signum);
 1062 
 1063         int cmp = intArrayCmp(mag, val.mag);
 1064         if (cmp==0)
 1065             return ZERO;
 1066         resultMag = (cmp>0 ? subtract(mag, val.mag)
 1067                            : subtract(val.mag, mag));
 1068         resultMag = trustedStripLeadingZeroInts(resultMag);
 1069 
 1070         return new BigInteger(resultMag, cmp*signum);
 1071     }

您所指的magand是的每个实例中的字段。它们不是按需计算的,它们. 他们的访问方法(不是函数调用)表明它只是访问存储位置。signumBigIntegerBigInteger

于 2013-08-09T20:24:40.497 回答