use*_*212 6 java int unboxing wrapper
例如,看看这段代码:
Integer myInt = new Integer(5);
int i1 = myInt.intValue();
int i2 = myInt;
System.out.println(i1);
System.out.println(i2);
Run Code Online (Sandbox Code Playgroud)
如您所见,我有两种方法可以将包装器中的整数值复制到原始值:
我可以使用拆箱
要么
我可以使用方法intValue()
那么......当已经取消装箱时,有什么方法需要一个方法?
除了弗兰克给出良好历史观点的答案之外,intValue()在某些情况下仍然需要使用今天.
请注意以下陷阱,表明您不能将其Integer视为int:
Integer i1 = new Integer(5);
Integer i2 = new Integer(5);
//This would be the way if they were int
System.out.println(i1 == i2); //Returns false
//This is the way for Integers
System.out.println(i1.intValue()==i2.intValue()); //Returns true
System.out.println(i1.equals(i2)); //Returns true
Run Code Online (Sandbox Code Playgroud)
返回
false
true
true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
705 次 |
| 最近记录: |