What is the point of the diamond operator (<>) in Java 7?

后端 未结 7 2345
渐次进展
渐次进展 2020-11-21 04:20

The diamond operator in java 7 allows code like the following:

List list = new LinkedList<>();

However in Java 5/6, I c

相关标签:
7条回答
  • 2020-11-21 05:05

    All said in the other responses are valid but the use cases are not completely valid IMHO. If one checks out Guava and especially the collections related stuff, the same has been done with static methods. E.g. Lists.newArrayList() which allows you to write

    List<String> names = Lists.newArrayList();
    

    or with static import

    import static com.google.common.collect.Lists.*;
    ...
    List<String> names = newArrayList();
    List<String> names = newArrayList("one", "two", "three");
    

    Guava has other very powerful features like this and I actually can't think of much uses for the <>.

    It would have been more useful if they went for making the diamond operator behavior the default, that is, the type is inferenced from the left side of the expression or if the type of the left side was inferenced from the right side. The latter is what happens in Scala.

    0 讨论(0)
  • 2020-11-21 05:09

    This line causes the [unchecked] warning:

    List<String> list = new LinkedList();
    

    So, the question transforms: why [unchecked] warning is not suppressed automatically only for the case when new collection is created?

    I think, it would be much more difficult task then adding <> feature.

    UPD: I also think that there would be a mess if it were legally to use raw types 'just for a few things'.

    0 讨论(0)
  • 2020-11-21 05:14

    The point for diamond operator is simply to reduce typing of code when declaring generic types. It doesn't have any effect on runtime whatsoever.

    The only difference if you specify in Java 5 and 6,

    List<String> list = new ArrayList();
    

    is that you have to specify @SuppressWarnings("unchecked") to the list (otherwise you will get an unchecked cast warning). My understanding is that diamond operator is trying to make development easier. It's got nothing to do on runtime execution of generics at all.

    0 讨论(0)
  • 2020-11-21 05:18

    When you write List<String> list = new LinkedList();, compiler produces an "unchecked" warning. You may ignore it, but if you used to ignore these warnings you may also miss a warning that notifies you about a real type safety problem.

    So, it's better to write a code that doesn't generate extra warnings, and diamond operator allows you to do it in convenient way without unnecessary repetition.

    0 讨论(0)
  • 2020-11-21 05:20

    The issue with

    List<String> list = new LinkedList();
    

    is that on the left hand side, you are using the generic type List<String> where on the right side you are using the raw type LinkedList. Raw types in Java effectively only exist for compatibility with pre-generics code and should never be used in new code unless you absolutely have to.

    Now, if Java had generics from the beginning and didn't have types, such as LinkedList, that were originally created before it had generics, it probably could have made it so that the constructor for a generic type automatically infers its type parameters from the left-hand side of the assignment if possible. But it didn't, and it must treat raw types and generic types differently for backwards compatibility. That leaves them needing to make a slightly different, but equally convenient, way of declaring a new instance of a generic object without having to repeat its type parameters... the diamond operator.

    As far as your original example of List<String> list = new LinkedList(), the compiler generates a warning for that assignment because it must. Consider this:

    List<String> strings = ... // some list that contains some strings
    
    // Totally legal since you used the raw type and lost all type checking!
    List<Integer> integers = new LinkedList(strings);
    

    Generics exist to provide compile-time protection against doing the wrong thing. In the above example, using the raw type means you don't get this protection and will get an error at runtime. This is why you should not use raw types.

    // Not legal since the right side is actually generic!
    List<Integer> integers = new LinkedList<>(strings);
    

    The diamond operator, however, allows the right hand side of the assignment to be defined as a true generic instance with the same type parameters as the left side... without having to type those parameters again. It allows you to keep the safety of generics with almost the same effort as using the raw type.

    I think the key thing to understand is that raw types (with no <>) cannot be treated the same as generic types. When you declare a raw type, you get none of the benefits and type checking of generics. You also have to keep in mind that generics are a general purpose part of the Java language... they don't just apply to the no-arg constructors of Collections!

    0 讨论(0)
  • 2020-11-21 05:22

    In theory, the diamond operator allows you to write more compact (and readable) code by saving repeated type arguments. In practice, it's just two confusing chars more giving you nothing. Why?

    1. No sane programmer uses raw types in new code. So the compiler could simply assume that by writing no type arguments you want it to infer them.
    2. The diamond operator provides no type information, it just says the compiler, "it'll be fine". So by omitting it you can do no harm. At any place where the diamond operator is legal it could be "inferred" by the compiler.

    IMHO, having a clear and simple way to mark a source as Java 7 would be more useful than inventing such strange things. In so marked code raw types could be forbidden without losing anything.

    Btw., I don't think that it should be done using a compile switch. The Java version of a program file is an attribute of the file, no option at all. Using something as trivial as

    package 7 com.example;
    

    could make it clear (you may prefer something more sophisticated including one or more fancy keywords). It would even allow to compile sources written for different Java versions together without any problems. It would allow introducing new keywords (e.g., "module") or dropping some obsolete features (multiple non-public non-nested classes in a single file or whatsoever) without losing any compatibility.

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