It has been pretty good on the whole, but don't like the explanations it gives on generics.
Then, i strongly recommend that you read the K&B book chapter on generics (and collections). Its enjoyable, easy and good for beginners. If you dont trust me, then ask all the folks in this forum or elsewhere --- they will tell you which is better.
Joined: Mar 31, 2011
Hmm, I suppose I trust you ;-)
Thanks for the advice, my mate has that book, so i'll have a read..
Rahul Sudip Bose,
K&B's book is great but I found the section on Generics somewhat confusing- mainly the parts that deal with that gives warnings. From my reading of it I got the impression that one only gets warnings if they pass a type safe Collection into a method with does not use generics and then the method adds something to it. However one gets a warning when they add anything to a non type safe Collection.
The book makes it clear that the first one gives a warning but it seems to be suggesting it's because a type safe list is been passed into a non safe list and then something is added, when in fact the warning is given because adding anything without using generics gives a warning. Of course it doesn't help that Eclipse gives it own warnings, which aren't java warnings, when using a Collection without a type!
So unless someone corrects me, be careful not to be mislead when reading the chapter.
Any other suggestions? I have read both books' chapters on Generics, but I am struggling too. Head First Java has a chapter on Generics and Collections. This was more approachable - I could understand it! - but I don't think it goes into the depth necessary for the exam.
Stephan, I am usually ok at using the collections classes from the API, the problem is if I had to write my own classes/methods (or if I had to predict what kind of warnings/errors the compiler would throw and why). I suppose practicing helps, as you said. So far, I haven't done much because I haven't found myself confidently past the overwhelmed "what the **** is this all about" stage yet
Cheers Dieter, I will give that a read over the weekend.
.... suddenly there is hope again...
Joined: Dec 12, 2009
Less reading and more reasoning!
Both compile but only one give a warning, but which one and why? View the compile warning as it's way of saying-"hey this is legal but that doesn't mean it's safe- don't be surprised if it blows up at runtime!!".
So lets look at the first example. list1 can only take a String object, which means that list2 will only contain String which is perfectly fine. When you get() an item out of the list it will return an object which you have to then cast to String for you to assign it to a String reference. No problems there.
Now the 2nd example. Here you have a list1 that can contain anything, including String. Now this list is assigned to another list that should only contain Strings so the compiler flags a warning to say what you are doing might not be safe. Why- because this now gets past the compiler even though list2 might not contain a String.
String str = list2.get(0);
This code also compiles, but with a warning, and runs fine. Adding anything to an untyped collection will cause a warning and the above example shows why. list1, the typesafe String list, list1, now contains an Integer! Just to be clear adding a String would also have given a warning.