所以,我们期望表达,
Object o1 = true ? new Integer(4) : new Float(2.0));
与之相同,
Object o2;
if (true)
o2 = new Integer(4);
else
o2 = new Float(2.0); 但是运行代码的结果会产生意想不到的结果。
// A Java program to demonstrate that we should be careful
// when replacing conditional operator with if else or vice
// versa
import java.io.*;
class GFG
{
public static void main (String[] args)
{
// Expression 1 (using ?: )
// Automatic promotion in conditional expression
Object o1 = true ? new Integer(4) : new Float(2.0);
System.out.println(o1);
// Expression 2 (Using if-else)
// No promotion in if else statement
Object o2;
if (true)
o2 = new Integer(4);
else
o2 = new Float(2.0);
System.out.println(o2);
}
}
输出:
4 4