public static void main(String[] args){
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
BigInteger sum = BigInteger.ZERO;
sum.add(one);
sum.add(two);
System.out.println(sum);
}
}
按照自动装箱和拆箱的特性应该等于3才对啊!为什么会是0呢?
难道不是自动装箱和拆箱的原理?那是为什么?渴望求解作者: 曹睿翔 时间: 2013-3-13 00:16
public class DuoTaiDemo{
public static void main(String[] args){
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
BigInteger sum = BigInteger.ZERO;
System.out.println( sum.add(one));
sum = sum.add(one); //主要原因是你没接收add()返回的值(BigInteger 类型),这就行了
sum = sum.add(two);
System.out.println( sum.add(two));
System.out.println(sum);
}
}
不涉及自动拆箱、装箱可以看API文档
addpublic BigIntegeradd(BigInteger val)返回其值为 (this + val) 的 BigInteger。//(this + val)这里是两个 BigInteger 对象相加作者: 黑马-郑玉元 时间: 2013-3-13 08:20
public class Test
{
public static void main(String[] args) throws Exception
{
调用了Integer的静态方法valueOf(这可能是编译器处理的)。
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}