2-13编程作业

2-13编程作业

public class HelloWorld {

    public static void main(String[] args) {

    // 定义int类型变量,值为100

int score = 100; 

// 创建Integer包装类对象,表示变量score1的值

Integer score1=score;

//Integer score1=Integer.value(score);

//Integer srore1=new Integer(score);

        

// 将Integer包装类转换为double类型

double one=new Integer(score1);

        

// 将Integer包装类转换为long类型

long two=score1.longValue();

        

// 将Integer包装类转换为int类型

int three=score1.intValue();

//打印输出

System.out.println("score对应的Integer类型的结果为:"+score1);

System.out.println("score对应的double类型的结果为:"+one);

System.out.println("score对应的long类型的结果为:"+two);

System.out.println("score对应的int类型的结果为:"+three);

}

}

我想请问老师 

// 将Integer包装类转换为double类型

double one=new Integer(score1);

这样写可以吗? 

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿满 2021-01-11 18:22:42

同学你好,score1已经是包装类型数据,new Integer(score1);这句代码是将score1自动转换int类型100,然后作为参数传入new Integer()中,又将int类型转换为包装类。最后赋值给double类型对象one时,自动拆箱并进行类型转换操作。

祝学习愉快~

好帮手慕阿满 2021-01-11 15:44:23

同学你好,double one=new Integer(score1);并不会出错,但是不符合此次练习的目的。将Integer包装类转换为double是希望使用score1.doubleValue();拆箱操作的方法。

祝学习愉快~



  • 提问者 rock221 #1

    double one=new Integer(score1);  那这样写的话就是自动转为成了包装类,又手动装箱了吗??

    2021-01-11 15:52:20
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师