黑马程序员 关于包装类的自动封箱与拆箱操作需要注意的地方

来源:互联网 发布:用记事本写c语言 编辑:程序博客网 时间:2024/04/27 17:45

------- android培训、java培训、期待与您交流! ----------

class Demo

{
        public static void main(String[] args) 
        {
                Integer in1=new Integer(100);
                Integer in2=new Integer(100);

                Integer in3=100; //当使用上面这两句话来创建Integer对象时,
                                          //如果值是在-128---127 范围内它不会新创建对象

                                           //如果不是在这个范围内,会重新创建对象
                Integer in4=100;
                System.out.println(in1==in2);// false   比较两个引用的地址,当然不同
                System.out.println(in3==in4); //true   如果in3 in4的值为1000 则为false
        }
}
此外
Integer是类  创建它的对象要使用new关键字在jdk1.5前:Integer in1=new Integer(10);  
在jdk1.5后可以使用自动封箱  将10包装成对象:Integer in1=10; 
                                                                 Integer in2=20;
在jdk1.5前   int num=in1.intValue()+in2.intValue();
Integer num=in1+in2;  int1+in2在操作时完成了自动的拆箱  将Integer对象的int值得到进行运算
                                等号右边运算后得到的结果又进行封箱操作。