Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Tags
#bloch-effective-java-2ed #java #java-generics
Question
Give an example how type safety could be compromised if it was legal to create arrays of parametrized types (but it is not legal).
Answer
  1. You could create an array of lists of strings:
    List<String>[] stringLists = new List<String>[1]; // this part is not legal and wouldn't compile
  2. Because arrays are covariant, you can store the array into a variable of Object[] type and then store an element of type List<Integer> (or really List<anything>) as an element of the original array without causing ArrayStoreException because at runtime List<String> and List<Integer> are simply lists
  3. So, we are in trouble - List<Integer> is stored where the compiler thought it was List<String>

Tags
#bloch-effective-java-2ed #java #java-generics
Question
Give an example how type safety could be compromised if it was legal to create arrays of parametrized types (but it is not legal).
Answer
?

Tags
#bloch-effective-java-2ed #java #java-generics
Question
Give an example how type safety could be compromised if it was legal to create arrays of parametrized types (but it is not legal).
Answer
  1. You could create an array of lists of strings:
    List<String>[] stringLists = new List<String>[1]; // this part is not legal and wouldn't compile
  2. Because arrays are covariant, you can store the array into a variable of Object[] type and then store an element of type List<Integer> (or really List<anything>) as an element of the original array without causing ArrayStoreException because at runtime List<String> and List<Integer> are simply lists
  3. So, we are in trouble - List<Integer> is stored where the compiler thought it was List<String>
If you want to change selection, open document below and click on "Move attachment"

pdf

owner: piotr.wasik - (no access) - Effective Java (Joshua Bloch), 2ed, p120

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.