Skip to content

Instantly share code, notes, and snippets.

@kh0ma
Created August 8, 2018 12:54
Show Gist options
  • Save kh0ma/842c418c183e547c386715d1c5565654 to your computer and use it in GitHub Desktop.
Save kh0ma/842c418c183e547c386715d1c5565654 to your computer and use it in GitHub Desktop.
Are you Generic?
import java.util.ArrayList;
import java.util.List;
/**
* @author <a href="mailto:khomenko.dp@gmail.com">Oleksandr Khomenko</a>
* <br>
*/
public class AreYouGeneric {
public static void main(String[] args) {
//EXTENDS
List<? extends String> lol = new ArrayList<>();
//lol.add(new Object()); does not compile
//lol.add("Lol"); does not compile
// WTF? We can add only children of String
for (String l : lol) {
//but go through strings
}
List<? super String> opa = new ArrayList<>();
//opa.add(new Object()); does not compile
opa.add("Lol");
// for (String o : opa) { does not compile
//
// }
for (Object o : opa) {
// can go only through Object
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment