static <T> T pick(T a1, T a2) { return a2; }
Serializable s = pick("d", new ArrayList<String>());
if I am making the reference to String or any other type. Compiler is throwing an error .
Could you please help me understand why does java infer it to Serializable?
a1
, you're passing "d"
of type String
.a2
, you're passing new ArrayList<String>()
of type ArrayList<String>
The nearest common supertype of String and ArrayList is Serializable
.
It would also work if you wrote:
Object s = pick("d", new ArrayList<String>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With