Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What do angle brackets in Java mean?

Angle Bracket in Java is used to define Generics. It means that the angle bracket takes a generic type, say T, in the definition and any class as a parameter during the calling. The idea is to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes, and interfaces.

What is tilde in Java?

Bitwise Complement Operator (~ tilde) The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for the bitwise complement is ~ (Tilde).


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.


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)


In IntelliJ IDEA, the ~ here:

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

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


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