集册 Java实例教程 取消装箱int和double值

取消装箱int和double值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

445
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
取消装箱int和double值

import java.util.ArrayList;

import java.util.List;


public class Unboxing {/*from n o w j a v a . c o m - 时  代  Java*/


    public static void main(String[] args) {


        Integer i = new Integer(-8);


        // 1. Unboxing through method invocation

        int absVal = absoluteValue(i);

        System.out.println("absolute value of " + i + " = " + absVal);


        List<Double> ld = new ArrayList<>();

        ld.add(3.1416); // PI is autoboxed through method invocation.
        /**
        n o w    j a v a  . c o m
        **/


        // 2. Unboxing through assignment

        double phi = ld.get(0);

        System.out.println("phi = " + phi);

    }


    public static int absoluteValue(int i) {

        return (i < 0) ? -i : i;

    }

}


展开阅读全文