Created
August 8, 2018 12:54
-
-
Save kh0ma/842c418c183e547c386715d1c5565654 to your computer and use it in GitHub Desktop.
Are you Generic?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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