Unexpected type resulting from the ternary operator

主宰稳场 提交于 2019-11-26 06:03:33

问题


I\'m trying to write a method which gets a double, verifies if the number has something after the dot and if it does—returns a double, if doesn\'t—returns an int.

public class Solution {
    public static void main(String[] args) {
        double d = 3.000000000;
        System.out.println(convert1(d));
        System.out.println(convert2(d));
    }

    static Object convert1(double d) {
        if(d % 1 == 0)
            return (int) d;
        else
            return d;
    }

    static Object convert2(double d) {
        return ((d%1) == 0) ? ((int) (d)) : d;
    }
}

Output:

3
3.0

So, everything I want happens in method convert1(), but doesn\'t happen in method convert2(). It seems as these methods must do the same work. But what I have done wrong?


回答1:


As the other answers have stated, this behavior is because both possible results of a ternary expression must have the same type.

Therefore, all you have to do to make your ternary version work the same way as convert1() is to cast the int to an Object:

static Object convert2(double d) {
    return ((d % 1) == 0) ? ((Object) (int) (d)) : d;
}



回答2:


You're seeing an effect similar to the one in this question.

Slightly different rules govern the way Java handles types with the ternary operator than with an if statement.

Specifically, the standard says:

The type of a conditional expression is determined as follows:

...

Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

...

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.

Flipping to that page of the standard, we see:

If either operand is of type double, the other is converted to double.

which is what's happening here, followed by autoboxing to a Double. It appears that no such conversion happens with the if statement, explaining the difference.

More broadly --- this isn't a very good idea. I don't think it's good design to return one of an int or a double depending on the value -- if you want to round something off, use Math.floor, and if you don't want decimals printed, use printf.

EDIT: I don't think it's a good idea to do hacky things to circumvent the regular numeric conversion system. Here's an idea that gives you a String directly, which appears to be what you want:

static String convert3(double d) {
    return ((d % 1 == 0) ? Integer.toString((int)d) : Double.toString(d));
}



回答3:


The ternary operator requires both result values be the same type, so the int undergoes an automatic (safe) widening cast to double.

The ternary is not exactly the same as its if "equivalent".




回答4:


To solve the problem with numbers after the dot:

public Object convert(double number){
  double whole = Math.floor(number);
  if(Math.abs(whole - number) < DELTA){
    return (int) number;
  }
  return number;
}

The DELTA is sufficiently small constant, to solve the problem with integers encoded in floating point format.

I have written the code from memory, but I think the idea behind it is clear.



来源:https://stackoverflow.com/questions/25230171/unexpected-type-resulting-from-the-ternary-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!