我的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.以下代码的输出结果是()。

<span style="font-size:18px;">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);
	}
}</span>

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://bbs.csdn.net/topics/390822163

http://www.educity.cn/wenda/371183.html

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

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

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

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

时间: 2024-08-03 23:41:48

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

矿Java开发学习之旅------&amp;gt;Java排序算法经典的二分法插入排序

一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为採用折半比較,就可以得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比較,就是在插入A[i]时,取A[i-1/2]的关键码值与A[i]的关键码值进行比較,假设A[i]的关键码值小于A[i-1/2]的关键码值.则说明A[i]仅仅能插入A[0]到A[i-1/2]之间.故能够在A[0]到A[i-1/2-1]之间继续使用折半比較:否则仅仅能插入A[i-1/2]到A[i-1]之间

我的Java开发学习之旅------&amp;gt;Java经典排序算法之归并排序

一.归并排序 归并排序是建立在归并操作上的一种有效的排序算法,该算法是採用分治法(Divide and Conquer)的一个很典型的应用.将已有序的子序列合并,得到全然有序的序列.即先使每一个子序列有序.再使子序列段间有序.若将两个有序表合并成一个有序表.称为二路归并. 归并过程为:比較a[i]和a[j]的大小.若a[i]≤a[j],则将第一个有序表中的元素a[i]拷贝到r[k]中,并令i和k分别加上1.否则将第二个有序表中的元素a[j]拷贝到r[k]中,并令j和k分别加上1.如此循环下去.直

我的Java开发学习之旅------&gt;Java使用ObjectOutputStream和ObjectInputStream序列号对象相关问题解决方法

今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554) at java.io.ObjectInputStream.readObject0(ObjectInputSt

我的Java开发学习之旅------&gt;Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常

今天在使用Java NIO的Channel和Buffer进行文件操作时候,报了java.nio.charset.MalformedInputException: Input length = 1异常,具体如下: java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:260) at java.nio.char

我的Java开发学习之旅------&gt;在Dos环境下Java内部类的编译和运行

习惯了在IDE工具上进行代码编写,连最基本的Javac命令和Java命令都忘记的差不多了,今天对一个Java内部类进行编译和运行的时候,就出糗了.IDE是把双刃剑,它可以什么都帮你做了,你只要敲几行代码,点几下鼠标,程序就跑起来了,用起来相当方便.你不用去关心它后面做了些什么,执行了哪些命令,基于什么原理.然而也是这种过分的依赖往往让人散失了最基本的技能,当到了一个没有IDE的地方,你便觉得无从下手,给你个代码都不知道怎么去跑. 首先我在C盘上编写了一个InnerClassTest.java代码

我的Java开发学习之旅------&gt;Java String对象作为参数传递的问题解惑

又是一道面试题,来测试你的Java基础是否牢固. 题目:以下代码的运行结果是? public class TestValue { public static void test(String str) { str="World"; //代码3 } public static void main(String[] args) { String string = "Hello"; //代码1 test(string); //代码2 System.out.println(

我的Java开发学习之旅------&gt;System.nanoTime与System.currentTimeMillis的区别

首先来看一道题:下面代码的输出结果是什么? import java.util.HashMap; import java.util.Map; public class HashMapTest { public static void main(String[] args) { Map<String, String> map=new HashMap<String, String>(); map.put(String.valueOf(System.currentTimeMillis())

我的Java开发学习之旅------&gt;使用循环递归算法把数组里数据数组合全部列出

面试题如下:把一个数组里的数组合全部列出,比如1和2列出来为1,2,12,21. (面试题出自<Java程序员面试宝典>) 代码如下: import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** * 把一个数组里的数组集合全部列出,比如1和2列出来为1,2,12,21 */ public class ListAll { public static void main(String[] args

我的Java开发学习之旅------&gt;求字符串中出现次数最多的字符串以及出现的次数

金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数,如有多个重复的则都求出. 此题的解题思路如下: 引入TreeSet:通过集合快速找到所有出现过的字符串 引入ArrayList:为了快速排序,再通过StringBuffer生成排序后的字符串 通过String的indexOf方法和lastIndexOf方法来计算每个字符串出现的次数最大值 使用HashMap