What does this tilde mean?

2019-01-10 13:19发布

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

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

What does the tilde in the generic identifier mean? I know it's the unary bitwise NOT operator, but I don't see an operand here.

Also, I tried compiling it and got an error. Am I just missing something?

标签: java tilde
4条回答
我想做一个坏孩纸
2楼-- · 2019-01-10 14:05

In IntelliJ IDEA, the ~ here:

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

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

查看更多
贪生不怕死
3楼-- · 2019-01-10 14:07

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.

查看更多
家丑人穷心不美
4楼-- · 2019-01-10 14:22

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

查看更多
smile是对你的礼貌
5楼-- · 2019-01-10 14: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)

查看更多
登录 后发表回答