I always heard (and thought) Java as a strongly typed language. But only recently I noticed something that I used almost daily: overload intand double.
I can write the following, and this is really Java code:
int i = 1;
double j = 1.5;
double k = i + j;
But if I have a method, one of the arguments of which is double, I need to specify it:
public static <K, V> V getOrDefault(K k, Map<K, V> fromMap, V defaultvalue) {
V v = fromMap.get(k);
return (v == null) ? defaultvalue : v;
}
When I call the above method on Map<String, Double>, the argument defaultvaluecannot be int:
getOrDefault(aString, aStringDoubleMap, 0);
getOrDefault(aString, aStringDoubleMap, 0d);
Why does Java overload intin double(as well as in addition to this) and then automatically add it to double? I think the answer is how Java performs operator overloading (i.e., Overloading occurs in the operator +, not from intto double), but I'm not sure.
, SO .