List<String> box = new ArrayList<>();
box.add("small");
box.addAll(new ArrayList<>());
in jdk7 box.addAll(new ArrayList<>())
will not compiled, but in jdk8 is will
anyone can give me a help to understand what's the difference between jdk7 & jdk8 in Generic?
The difference is that Java 8 introduced polyexpressions.
These are expressions whose type is left somewhat undetermined, but is determined by context of how the expression is used.
new ArrayList<>()
is a polyexpression. On its own, it could be a list with any element type: the compiler "waits and sees" before it decides on the type.
Java 7 didn't support polyexpressions. It would consider new ArrayList<>()
to be new ArrayList<Object>()
, and thus incompatible with box.addAll
.
Without polyexpressions, lambdas and streams would have been incredibly awkward.
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