我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换

简介: 今天看到两个面试题,居然都做错了。通过这两个面试题,也加深对三目运算是的自动类型转换的理解。 题目1.以下代码输出结果是()。 public class Test { public static void main(String[] args) { int a=5; System.

今天看到两个面试题,居然都做错了。通过这两个面试题,也加深对三目运算是的自动类型转换的理解。

题目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 byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.

  • If one of the operands is of type T where T is byteshort, or char, and the other operand is a constant expression (§15.28) of type int whose 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 ByteShort, or Character, and the other operand is a constant expression of type int whose 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://www.educity.cn/wenda/371183.html



==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================



相关文章
|
23小时前
|
自然语言处理 Java
Java中的行为驱动开发(BDD)实践
Java中的行为驱动开发(BDD)实践
|
23小时前
|
设计模式 Java 测试技术
Java后端开发的最佳工程实践与规范
Java后端开发的最佳工程实践与规范
|
1天前
|
自然语言处理 Java 测试技术
Java中的行为驱动开发(BDD)方法论解析
Java中的行为驱动开发(BDD)方法论解析
|
1天前
|
存储 安全 Java
基于Java的区块链数字身份认证系统设计与开发
基于Java的区块链数字身份认证系统设计与开发
|
1天前
|
Java 应用服务中间件 API
如何安装与使用Java EE 8、Servlet 3.0及Apache Maven进行高效开发
搭建高效Java EE 8开发环境,包括安装JDK、选择WildFly或Payara Server作为应用服务器,以及安装Apache Maven。使用Maven创建Servlet 3.0 Web项目,编写 HelloWorldServlet,打包部署到服务器,通过访问特定URL测试应用。这一流程助力开发者实现快速原型和大型项目开发。【7月更文第1天】
36 0
|
1天前
|
前端开发 Java 开发工具
Java GUI编程:跨平台应用的设计与开发
Java GUI编程:跨平台应用的设计与开发
|
1天前
|
机器学习/深度学习 人工智能 Java
Java与AI集成开发:机器学习模型部署
Java与AI集成开发:机器学习模型部署
|
2天前
|
前端开发 JavaScript Java
java常用数据判空、比较和类型转换
java 开发中我们经常会用到的数据判空、数据比较和不同数据之间的类型转换,尤其数据判空可以让我们避免经常会出现 NullPointerException 空指针异常报错。
18 4
|
4天前
|
缓存 JSON Java
使用Java进行RESTful API开发的最佳实践
使用Java进行RESTful API开发的最佳实践
|
4天前
|
缓存 NoSQL Java
Redis系列学习文章分享---第四篇(Redis快速入门之Java客户端--商户查询缓存+更新+双写一致+穿透+雪崩+击穿+工具封装)
Redis系列学习文章分享---第四篇(Redis快速入门之Java客户端--商户查询缓存+更新+双写一致+穿透+雪崩+击穿+工具封装)
9 0