What does a tilde in angle brackets mean when creating a Java generic class?

前端 未结 4 790
醉酒成梦
醉酒成梦 2020-12-02 11:06

I was reading through some JMockit examples and found this code:

final List actualItems = new ArrayList<~>();

What d

相关标签:
4条回答
  • 2020-12-02 11:14

    I think that is shorthand to mean whatever the type is, in this case OrderItem.

    0 讨论(0)
  • 2020-12-02 11:23

    If there wasn't a tilde, I'd say, the code was already Java 7. Java 7 allows the diamond operator so this is/will be legal Java code:

    Map<String, List<String>> map = new HashMap<>();
    

    (but - no tilde with this syntax)

    0 讨论(0)
  • 2020-12-02 11:36

    It is just a shorthand for "same as in declaration".

    Some IDEs, e.g. IntelliJ use this too.

    The files on disk do not have this notation, which is only a compaction in the IDE GUI.

    0 讨论(0)
  • 2020-12-02 11:37

    In IntelliJ IDEA, the ~ here:

    Set<String> associations = new LinkedHashSet<~>();
    

    means String, which is the same as in the declaration on the left side.

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