不可变类

BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);			//0
		
//正确的做法
total = total.add(fiveThousand);
total = total.add(fiftyThousand);
total = total.add(fiveHundredThousand);
System.out.println(total);			//555000

 

    不可变类:
     String、BigDecimal、BigInteger以及包装器类型:Integer、Long、Short、Byte、Character、

     Boolean、Float 和Double 的实例是不可变的。
     你不能修改现有实例的值,对这些类型的操作将返回新的实例。不可变类型更容易设计、实现和使用;

     它们出错的可能性更小,并且更加安全。

你可能感兴趣的:(不可变类)