5

I have two methods in the Item Class:

public void setValue(String v);
public void setValue(Double v);

and I want to use Conditional Operator to setVAlue in another class:

String str = ...
Double dbl = ...
item.setValue((condition) ? str : dbl);

but compiler says:

cannot find symbol
symbol  : method setValue(java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>)

I think compiler uses the nearest common superclass (super interface) of Double and String as type of conditional Operator. but why?

Farvardin
  • 5,336
  • 5
  • 33
  • 54
  • 3
    The types must unify (e.g. be the same). The overload Java chooses to call is picked *during* the compilation. – user2864740 Jul 26 '14 at 05:23

3 Answers3

13

Because to do anything else wouldn't make any sense. The ternary conditional operator must return some value of some specific type -- all expressions must result in a specific type at compile time. Further, note that overload resolution happens at compile time, as well. The behavior you are trying to invoke here (late binding) doesn't exist in this form in Java.

The type of the expression must be compatible with the true and false subexpressions. In this case, the nearest common ancestor class is Object, and you don't have a setValue(Object) overload.

This is the simplest way to rewrite what you have in a working way:

if (condition) {
    item.setValue(str);
} else {
    item.setValue(dbl);
}

You could also provide a setValue(Object) overload that inspects the type of object passed and delegates to the appropriate setValue() overload, throwing an exception if the type is not acceptable.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Yep yep +1. I was about to reply =/ – Breno Inojosa Jul 26 '14 at 05:24
  • Small note, it would compile not only if provided with `setValue(Object)` but any of the other types listed (`setValue(Serializable)`, `setValue(java.lang.Comparable extends Object>)` etc.) but if multiple were provided, it would need to to be cast to make the call disambiguous. – vandale Jul 26 '14 at 05:37
  • @vandale Indeed, I thought about adding that but figured that might be too much to dump on the OP all at once. But I'm glad you did mention it. – cdhowie Jul 26 '14 at 05:47
2

The Java Language Specification says

[...]

Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2).

In your case, let's take T1 to be String and T2 to be Double.

From the JLS, about the least upper bound (lub)

The least upper bound, or "lub", of a set of reference types is a shared supertype that is more specific than any other shared supertype (that is, no other shared supertype is a subtype of the least upper bound).

You can continue reading the JLS to workout the exact way the lub is calculated, but from the definition above we can see that what's provided in the compiler error message makes sense.

Remember, the ternary operator is used as a single expression with a single value. That value must have a type at compile time. The JLS must therefore specify rules for it.

Here's a related question/answer

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

IF you want to go with the ternary operator only following code will solve your problem. For testing I have put false as the default value you can put your conditional expression.

public class st1 {
    public static void main (String []args) {
        Item i = new Item();
        i.setValue (false?"test":0.0);
    }
}

class Item {
    private String str;
    private double d;
    public void setValue (Object str) {
        try {
            d = Double.parseDouble (str.toString());
            System.out.printf ("Double type : %f", d);
        } catch (NumberFormatException ne) {
            this.str = str.toString();
            System.out.printf ("String type : %s", this.str);
        }
    }
}
iammurtaza
  • 957
  • 3
  • 16
  • 31