我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换
今天看到两个面试题,居然都做错了。通过这两个面试题,也加深对三目运算是的自动类型转换的理解。
题目1.以下代码输出结果是()。
public class Test {
public static void main(String[] args) {
int a=5;
System.out.println("value is :"+((a<5)?10.9:9));
}
}
A.编译错误 B.10.9 C.9 D.以上答案都不对
我不假思索的就选了C,认为这题目也太简单了吧。而答案却是:D.以上答案都不对。原来我中了此题目的陷阱了。
解析:三目运算表达式(expression1 ? expression2 : expression3),即本题中的表达式:((a<5)?10.9:9),第二个表达式为10.9,第三个表达式为9。这是Java会根据运算符的精度进行自动类型转换。由于10.9的原因,9也会自动变成9.0。因此此题的真正输出为
9.0 。题目2.以下代码的输出结果是()。
public class Test {
public static void main(String[] args) {
char x='x';
int i=10;
System.out.println(false?i:x);
System.out.println(false?10:x);
}
}A. 120 x B.120 120 C.x 120 D.以上答案都不对
答案为:A.120 x。
解析:
int i=10;中的i是一个变量,因此,第一个输出x被提升为int型,x的int值为120,所以第一个输出为120。
至于第二个输出,Java编程规范中提到:当后两个表达式有一个是常量表达式时,另外一个类型是T时,而常量表达式可以被T表示时,输出结果是T类型。所以,因为10是常量,可以被char表示。输出结果是char型,所以输出为x。
System.out.println(true?100:x); 这句的输出结果为:d。因为d是100对应的char值。
下面是Oracle官方中的一段原文:
15.25.2. Numeric Conditional Expressions
Numeric conditional expressions are standalone expressions (§15.2).
The type of a numeric conditional expression is determined as follows:
If the second and third operands have the same type, then that is the type of the conditional expression.
If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7)
to T, then the type of the conditional expression is T.If one of the operands is of type
byteorByteand the other is of typeshortorShort,
then the type of the conditional expression isshort.If one of the operands is of type T where T is
byte,short,
orchar, and the other operand is a constant expression (§15.28)
of typeintwhose value is representable in type T, then the type of the conditional expression is T.If one of the operands is of type T, where T is
Byte,Short,
orCharacter, and the other operand is a constant expression of typeintwhose value is representable in the type Uwhich is the result of applying unboxing conversion to T,
then the type of the conditional expression is U.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).翻译过来就是:
如果第二和第三个操作数在可以转换为数值类型时,会有以下几种情况:
操作数其中一个是 byte 或 Byte 类型,而另一个是 short 或 Short 类型,那么这个表达式就是 short 类型
操作数中的一个是类型 T (T 可以是 byte、short 或者是 char 类型),而另一个是 int 类型的常数,其可以用 T 类型来表示时,那么这个表达式就是 T 类型
操作数中的一个是 Byte 类型,而另一个是 int 类型的常数,其可以用 byte 类型来表示,那么这个表达式就是 byte 类型
操作数中的一个是 Short 类型,而另一个是 int 类型的常数,其可以用 short 类型来表示,那么这个表达式就是 short 类型
操作数中的一个是 Character 类型,而另一个是 int 类型的常数,其可以用 char 类型来表示,那么这个表达式就是 char 类型
否则,双目数值提升(binary numeric promotion)会被用于操作数的类型中,条件表达式的类型是第二个和第三个操作数提升后的类型。注意:双目数值提升时进行拆箱转换和值集转换(value set conversion)关于更多关于三目运算符(Conditional Operator
? :)的介绍可以查看Oracle公司的官方文档,地址如下:http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25关于Boxing Conversion的介绍查看Oracle公司的官方文档,地址如下:
http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7
下面两篇文章也有一定的参考性:
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换的更多相关文章
- 我的Java开发学习之旅------>Java经典排序算法之归并排序
一.归并排序 归并排序是建立在归并操作上的一种有效的排序算法,该算法是採用分治法(Divide and Conquer)的一个很典型的应用.将已有序的子序列合并,得到全然有序的序列.即先使每一个子序列 ...
- 矿Java开发学习之旅------>Java排序算法经典的二分法插入排序
一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为採用折半比較,就可以得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比較 ...
- 我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法
本文参考: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html http://www.blogjava.net/ ...
- 我的Java开发学习之旅------>工具类:将播放器的进度值转换成相应的时间格式
在我的博客<我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法,地址:http://blog.csdn.net/ouyang_pen ...
- 我的Java开发学习之旅------>在Dos环境下Java内部类的编译和运行
习惯了在IDE工具上进行代码编写,连最基本的Javac命令和Java命令都忘记的差不多了,今天对一个Java内部类进行编译和运行的时候,就出糗了.IDE是把双刃剑,它可以什么都帮你做了,你只要敲几行代 ...
- 我的Java开发学习之旅------>Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常
今天在使用Java NIO的Channel和Buffer进行文件操作时候,报了java.nio.charset.MalformedInputException: Input length = 1异常, ...
- 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法
今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...
- 我的Java开发学习之旅------>Java String对象作为参数传递的问题解惑
又是一道面试题,来测试你的Java基础是否牢固. 题目:以下代码的运行结果是? public class TestValue { public static void test(String str) ...
- 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值
ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...
随机推荐
- springboot 2.0.8 跳转html页面
springboot项目创建链接 https://blog.csdn.net/q18771811872/article/details/88126835 springboot2.0 跳转jsp教程 h ...
- golang:mgo剖析之Session
golang操作mongo使用的包是"gopkg.in/mgo.v2",coding过程中需要并发读写mongo数据库,简单观摩了下源码,记录下自己的一些理解,如有错误,敬请斧正. ...
- 详解UIView的frame、bounds和center属性
From: http://ios.wpjam.com/2011/08/29/uiview-frame-bounds-center/ 1.概要 翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下 ...
- web.xml文件的 xsd引用(或dtd引用)学习
1. 为什么web.xml会有不同版本的xsd引用: JDK依赖变化: 或 servlet(JAVA EE)自身API的改变: 2. 为什么会有dtd和xsd两个版本的区别 我是在这篇文章中看到的,作 ...
- SVN merge 三种方式
1.Merge a range of revisions 2.Reintegrate a branch 3.Merge two different trees ———————————————————— ...
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- Win7如何修复开机画面
将下面文件保存为"修复Win7开机画面.bat"双击运行即可 bcdedit /set {current} locale zh-CN
- Android Touch事件传递机制具体解释 下
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38025165 资源下载:http://download.csdn.net/detail/yu ...
- Git 学习之--安装配置GitHub
楼主今天学习了一下Git的使用,而且Androdi studio 下加入了Git插件,成功提交项目到自己Github个人主页 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- websocket使用ssl 证书,开启加密服务
参考文章:https://fzambia.gitbooks.io/centrifugal/content/deploy/certificates.html TLS certificates TLS/S ...