149

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?

4 Answers 4

178

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It was an Intellij screenshot.
Just for personal clarification - IntelliJ produces Java source files with this notation, iaw, source files with illegal Java content??? Or is this just the view of the source and the saved file is correct?
@Andreas_D: It is just a view. The files on disk are ok.
And actually, the tilde is grayed (to indicate that it's different), and if you click on it, it'll expand to the full definition.
This can be disabled in the intelij settings for editor/general/Code Folding.
20

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)

Comments

15

In IntelliJ IDEA, the ~ here:

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

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

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.