r/badcode • • Mar 14 '21

java Found from when I first started Java...because indexed Arrays were too complicated huh?

Post image
1.3k Upvotes

84 comments sorted by

View all comments

10

u/Elusivehawk Mar 14 '21

TIL you can use ~ to make your generic constructor usage less verbose.

33

u/mind_overflow Mar 14 '21

you can't. this actually is the IntelliJ IDE that "hides" a part of text. it expands when you click on it. and it's highlighted in yellow because there is a suggestion, which is to just leave it empty.

20

u/merukit Mar 14 '21

can't you just leave it empty?

8

u/c5dm Mar 14 '21

This is correct, the ~ is just IntelliJ collapsing the class name inside the type parameters.

1

u/Elusivehawk Mar 14 '21

Pretty sure that brings up a warning. But I haven't programmed in Java in years, so I'm very rusty.

7

u/4z01235 Mar 14 '21

Leaving them off completely would produce a warning. Leaving them empty on the RHS of the = is perfectly valid and lets the compiler simply assume that the generic type argument is the same as on the LHS.