Java自动装箱与拆箱

//-------------------------------------------------------------------------

// Header

//

// Copyright (c) 1998-1998 GEMALTO group. All Rights Reserved.

//

// This software is the confidential and proprietary information of 

// GEMALTO.

//

//  Project name:  CSTK

//

//  Platform    :  Java virtual machine

//  Language    :  JAVA 1.6.0

//  Devl tool   :  Eclipse

//

//  Workfile

//  version: 1.0

//  Date   : 2009-7-17

//  Author : 

//  Modtime:

//

//  Original author:

//

//-------------------------------------------------------------------------

// GEMALTO MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF

// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED

// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A

// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. GEMALTO SHALL NOT BE

// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,

// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.

//

// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE

// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE

// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT

// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE

// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE

// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE

// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). GEMALTO

// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR

// HIGH RISK ACTIVITIES.

//-------------------------------------------------------------------------

 

package org.zdxue.javase.lang;

 

/**

 * Java自动装箱与拆箱

 *

 * @version           1.0 - 2009-7-17

 * @author            

 */

public class Boxing_Unboxing

{

    public static void main(String[] args)

    {

        Integer a = 1024; //自动装箱(编辑器会隐式处理为:Integer a = new Integer(1024); )

        Integer b = 1024; //自动装箱(编辑器会隐式处理为:Integer b = new Integer(1024); )

        int c = 1024; //基本数据类型

        int d = a; //自动拆箱(编辑器会隐式处理为:int d = a.intValue(); )

        double e = a;//自动拆箱(编辑器会隐式处理为:int d = a.doubleValue(); )

        short f = a.shortValue(); //不可以写成:short f = a; 因为隐式转换会丢失精度,必须向上(高精度)转型

        boolean test = a < b || a == b || a > b; //对象运算

        System.out.println(test);

        System.out.println(a == c); //自动将a拆箱后,与c进行计算

        System.out.println(d);

        System.out.println(e);

        System.out.println(d == e);

        System.out.println(f);

 

        //---------------------------------------------

        /**

         * int值在-128~127之间,装箱时,在内存堆中只new一次,则h1,h2指向同一个对象,因此地址== true

         * 但是不在上述的范围时(-128~127),会在内存堆中各个new一次,也就是说,引用地址是不同的,因此== false

         */

        Integer h1 = 127;

        Integer h2 = 127;

        System.out.println("h1=h2 ->" + (h1 == h2)); //在范围内,true

 

        Integer h3 = 128;

        Integer h4 = 128;

        System.out.println("h3=h4 ->" + (h3 == h4)); //不在范围内,false

 

        //--------------------------------------

        Long l1 = 128L;

        Long l3 = 128L;

        long l2 = 100;

 

        Long l5 = 127L;

        Long l4 = 127L;

 

        System.out.println("l1==l2 -> " + (l1 == l2));

        System.out.println("l1==l3 -> " + (l1 == l3)); //不在范围内,false

        System.out.println("l4==l5 -> " + (l4 == l5)); //在范围内(-128~127),true

 

        //---------------- 注意 ------------------------------------

        Integer i = null;

        //int j = i.intValue();

        int j = i; //编译上没有问题,但是已经隐含NullPointerException, 编译器会处理为:ing j = i.intValue();

        System.out.println(j);

        //---------------------------------------------------------

 

        int d0 = 100;

        Double d1 = (double)d0; //注意与下面的区别

        double d2 = d0;

    }

}

 

你可能感兴趣的:(java,eclipse,C++,c,AIR)