Ruben Soto wrote:\
In this case, the compiler is preventing you from declaring an A<K> reference where K is out of bounds. You could argue that the compiler could actually let this code be legal, and check on the bounds when an instance of A is instantiated, but this is not the actual problem in this case. The actual problem is that now k is a reference of A which might be out of bounds, and you might be able to add to an actual A instance (which will be within bounds) passed as a parameter to the method an element of type K, which will break the type safety of the collection (since K is out of bounds.) This couldn't happen when the parameter had a wildcard, since you can't add elements through generic references with wildcards.
Let me know if you see any flaw in my reasoning, Ankit. And again, thanks for the great explanation.
Originally posted by Clay Chow:
So another question on objects:
Since an array (whether it be an array of primitives or objects) is an object itself, then a 2D array would create (number of elements in first dimension plus one) objects ?
For example, in the below code.
Line 1 creates 4 objects.
Line 2 creates 1 object (?)
After line 4, one object is eligible for gc (the original array at x).
However, after line 5, there is still only 1 object eligible for gc (since there was not an array at x2 originally).
Thanks in advance for your help!