所以我访问了一个网站,上面写着整数不允许使用它进行计算,+ - */% ^ 等。
但是我尝试过
Integer n=new Integer(8);
n+=2;
Integer m=new Integer(10);
System.out.println(n+m);
它打印出 20。所以我有点困惑。我知道这是一个菜鸟问题,但我真的很想得到这个,但我不知道我错过了什么。那么这实际上意味着“整数不允许用它进行计算”
谢谢。
编辑:@people 请求:Site providing accurate information?
最佳答案
Java 1.5+ 自动装箱(和拆箱)原始类型 - 这称为自动装箱。
您遇到的网站可能讨论过 Java 1.5 之前的内容:
In code prior to Java version [1.4, edited], you had to do a lot of manual tedious converting back and forth from primitive int to Integer Object, and similarly for byte/ Byte, char/Character, short/ Short, long/ Long, float/ Float and double/ Double. (http://mindprod.com/jgloss/autoboxing.html)
关于java - 用整数计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285973/