我是编程的新手.告诉我Integer x= 59;和Integer x= new Integer (59);
他们之间的区别
他们基本上做同样的事情,我得到输出任何一种方式.
public class WrapperClass
{
public static void main(String args[])
{
Integer x= 59; //
byte y= x.byteValue();
System.out.println(y);
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class WrapperClass
{
public static void main(String args[])
{
Integer x = new Integer (10);
byte y= x.byteValue();
System.out.println(y);
}
}
Run Code Online (Sandbox Code Playgroud)
差别不大.当另一个方法调用构造函数时,Autoboxing(Integer x = 59;)将调用Integer.valueOf( 59 );.对缓存有轻微影响(valueOf可能为两个相等的值提供相同的对象引用,new不会),但不多.
并且只是为了确保:Autoboxing/valueOfMIGHT在用两个相等的值调用两次时给你相同的对象引用(至少如果你的值在-128到127之间),但这仍然是一个非常非常糟糕的主意.通过比较两个Integer对象==.
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |