Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
above code compiles fine, the answer to it from the link is as below
"List of Integer types can be treated as some Collection of Number types through the wildcard instantiation. If you think about it, you'll see that there is no conflict here. A List is certainly a Collection. And all we're doing is widening the type by which we can read the elements from Integer to Number".
if so why below code does not compile
How wildcards can make difference here.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Generics types must be the same. Period. You can't do this:
List<Number> = new ArrayList<Integer>();
It only works with
List <Number> = new ArrayList<Number>(); or List<Integer> = new ArrayList<Integer>();
It might be strange because you CAN do it with arrays:
Number num = new Integer;
Well, the explanation is a little long, but short: Generics verification only occurs at compiling time. At runtime, java use these classes as if they were no generic restriction. Array does use types at runtime.
So, why is it important?
What happens if I do this?
List <Number> = new ArrayList<Integer>;
See what happened? It should be legal, but it is wrong. At runtime, java won't notice it when you add it, but it will explode when you retrieve it.
When you use wildcard using extends, you're saying that you won't add anything to the list, so you can assign it. By the way, you can add items if you're using wildcard with super, just think about why.
Hi Ramesh..i am also preparing SCJP 1.5 I want your advice on this regarding what are the inportant topics to be covered that appear mostly in the exam. I will also appreciate if you can provide me with good URL's and e-books. My gmail id is firstname.lastname@example.org. Thanks in advance.
What a show! What atmosphere! What fun! What a tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss