Can you please run the below and explain?

Object o = true ? new Integer(1) : new Double(2.0);

System.out.println(o);

I found that surprising as someone would expect 1 to be printed and not 1.0

解决方案

It's not a surprise at all, although it might seem like one. The behaviour is specified in JLS §15.25 - Conditional Operator:

Otherwise, if the second and third operands have types that are

convertible (§5.1.8) to numeric types, then there are several cases:

If one of the operands is of type byte or Byte and the other is of

type short or Short, then the type of the conditional expression is

short.

[...]

Otherwise, binary numeric promotion (§5.6.2) is applied to the operand

types, and the type of the conditional expression is the promoted type

of the second and third operands.

Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

So the Integer and Double types are unboxed to their respective primitive counterparts - int and double, as a process of binary numeric promotion. And then the type of the conditional operator is the promoted type of int and double, which is double. Hence the result is 1.0. And of course the final result is then boxed back to Double.

用条件运算符编写java程序,使用条件运算符的奇怪java行为。这是一个错误吗?...相关推荐

  1. java程序打包为jar_将Java应用程序打包为一个(或胖)JAR

    java程序打包为jar 这篇文章将针对一个有趣但非常强大的概念:将您的应用程序打包为单个可运行的JAR文件,也称为一个或胖 JAR. 我们习惯了大型WAR归档文件,其中包含所有打包在某些常见文件夹结 ...

  2. 树莓派Java程序运行_树莓派上Java程序作为linux服务并开机自动启动

    http://www.iigrowing.cn/shu_mei_pai_shang_java_cheng_xu_zuo_wei_linux_fu_wu_bing_kai_ji_zi_dong_qi_d ...

  3. Java程序应用实例:“你好 Java”

    编写Java源程序 Java 源程序可以使用任何一个文本编辑器来编写,这里以 Windows 下的记事本为例. 例 1 (1) 新建一个空白记事本,然后如实地输入下列内容. /* *第一个java程序 ...

  4. java程序员工资有多少?java程序员现状如何?

    现在java程序员的日子好过吗?他们的工资一般都是多少呢?现状如何呢?java程序员烂大街了吗?带着这些问题,让我们一起通过下面的文章来了解一下吧. 1.java程序的现状 一句话,加班,工资高. 这 ...

  5. 一年经验的java程序员薪资有多少?java收入分级

    Java编程可以应用到网站建设.游戏开发.移动系统开发等多个领域.所以随着科学技术进步电子产品不断更新问世,越来越成为企业重视的开发人才.这也是Java编程在近几年越发火爆的原因.一年经验的java程 ...

  6. 编写java程序的三步骤_帮助Java小白涨知识的教程(三)(运行HelloWorld程序)

    总共有三个步骤:编写----------编译-----------执行 要用的文件:文字编译器(notepad++\editplus...)------------------------------ ...

  7. java编写应用程序_为您的Java应用程序编写数据驱动的测试

    java编写应用程序 JUnit是一个功能非常强大的测试框架,它不仅为其用户提供了编写快速简便的测试的功能,而且还为用户提供了扩展它并使其按其期望的方式工作的机会. 在JUnit之上构建了许多框架,这 ...

  8. java程序笑脸怎么打_我编写的JAVA程序为什么在编译执行后会先打出一个笑脸呢?...

    嗯,我个人认为,从键盘输入再输出意义不是很大,如果楼主有C的基础,那么这个输入将是一个很好练习,因为java的输入和输出与C有很大的区别,废话不多说 首先,无论是在Eclipse的控制台上还是CMD模 ...

  9. java程序运行结果题_(Java程序设计)试题

    装 订 线 内 不 答 题 要 二.多选题 (每题2分,共10分) . A.Java 语言是面向对象的.解释执行的网络编程语言. B.Java 语言具有可移植性,是与平台无关的编程语言. C.Java ...

  10. Java程序员必看!java值类型和引用类型的区别

    招聘职位需求套路摘举 以下几个公司的招聘需求:(from lagou) 1.Java开发岗位 职位诱惑: 福利好待遇佳,技术氛围浓,有大牛带成长快. 职位描述: 有赞2021校招官方网申地址(请在官网 ...

最新文章

  1. sublime Text 开发工具
  2. es存在某个字段的查阅_ElasticSearch系列02:ES基础概念详解
  3. Spring的Hello, world,还有拿来主义
  4. 集合框架-Collection与List集合
  5. 迭代器的设计原则和Iterator Traits的作用与设计
  6. android tabhost 生命周期,FragmentTabHost + FragmentLayout布局框架,Fragment生命周期
  7. 查找与清除线程插入式木马
  8. AutoCAD2018_输出与打印
  9. 电信版行货黑莓9630修复历险记
  10. win7家庭版升级旗舰的密钥
  11. GridWorld实训总结报告
  12. 易语言打开c 程序,易语言调用word 易语言按钮打开一个word文档
  13. bootstrap-table动态添加列、动态添加行、单元格点击横向、竖向统计
  14. Debian10安装教程
  15. 级联rc滤波_了解无源RC滤波器,看完这篇你就懂了(二)
  16. 【基础练习】【DFS】codevs2806 红与黑题解
  17. mothur trim.seqs 去除PCR引物
  18. 【Linux】USB驱动程序
  19. Python实现机器学习二(实现多元线性回归)
  20. 利用爬虫从一个百度贴吧页面下载图片

热门文章

  1. 最简单的 SAP Cloud Application programming CDS 练习
  2. NgRx 和 Angular CLI 版本不一致的错误
  3. 关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
  4. SAP Spartacus 的延迟加载 Lazy load 设计原理
  5. SAP Spartacus user role页面的checkbox设计明细介绍
  6. Github提交记录里用户超链接无法显示的问题
  7. SAP Spartacus CmsPage的加载逻辑
  8. SAP Cloud Platform Neo环境的权限管理
  9. ERP text object hard code
  10. Tax debug and BP number external generation