Is there a difference between explicitly putting the type into the diamond operator vs letting java figure it out?

前端 未结 4 1742
终归单人心
终归单人心 2020-12-11 18:51

Is there any difference between initialization via:

MyWrapper wrapper = new MyWrapper();

vs initialization via:

相关标签:
4条回答
  • 2020-12-11 19:30

    Before Java 7, the former was required. Now with type inferencing, the latter is preferred.

    0 讨论(0)
  • 2020-12-11 19:43

    It is largely because while Java 7 is the current version, a lot of code still uses Java 6, which does not support the latter form.

    In essence, Java 7 compilers are better at infering generic types.

    0 讨论(0)
  • 2020-12-11 19:51

    The latter is only available since Java 7. That's why you often see the former. The latter is equivalent, and shorter.

    0 讨论(0)
  • 2020-12-11 19:52

    JDK 7 allows to not redefine this "redundant" diamond at initialization time.

    It's just a shorter syntax.

    With JDK <= JDK 6, the latter doesn't work, you have to use the former.

    0 讨论(0)
提交回复
热议问题