我的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的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...
随机推荐
- CSS 布局整理(************************************************)
1.css垂直水平居中 效果: HTML代码: <div id="container"> <div id="center-div">&l ...
- 深入浅出 Cocoa 之 Core Data(1)- 框架详解
深入浅出 Cocoa 之 Core Data(1)- 框架详解 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 Core data 是 Cocoa 中处 ...
- C#规范整理·泛型委托事件
基于泛型,我们得以将类型参数化,以便更大范围地进行代码复用.同时,它减少了泛型类及泛型方法中的转型,确保了类型安全.委托本身是一种引用类型,它保存的也是托管堆中对象的引用,只不过这个引用比较特殊,它是 ...
- MyEclipse出错解决
错误信息: Deployment failure on Tomcat 6.x. Could not copy all resources to C:\Tomcat 6.0\webapps\JavaP ...
- Discussion about z pre-pass
Z pre-pass In the rendering Process, the first pass render to a depth buffer to get the front layer ...
- Web终端之使用shellinabox在浏览器进行ssh登录
shellinbox有一个内建的web server作为基本的web ssh client,允许你通过指定的端口访问linux服务器的ssh shell,只要你的浏览器支持AJAX/JS/CSS就可以 ...
- 身份证实名认证接口调用实例(PHP)
基于php的身份证实名认证接口调用代码实例,身份证实名认证接口申请:https://www.juhe.cn/docs/api/id/103 <!--?php // +-------------- ...
- TP如何进行批量查询
public function getUserInfo($uid){ if(is_null($uid) || empty($uid)){return false;} if(is_arr ...
- Vue 响应式属性
本文参考自:https://www.w3cplus.com/vue/vue-reactivity-and-pitfalls.html 1.概述 当创建一个Vue实例时,每个数据属性.组件属性等都是可以 ...
- frame框架及其实例
框架概念 : 谓框架便是网页画面分成几个框窗,同时取得多个 URL.只需要 <FRAMESET> <FRAME> 即可,面所有框架标记需要放在一个总起的 html 档,这个档案 ...