File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Good generics tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Good generics tutorial" Watch "Good generics tutorial" New topic
Author

Good generics tutorial

Dan Webber
Greenhorn

Joined: Mar 31, 2011
Posts: 5
Hi,

I'm currently revising for my ocjp, but I'm finding generics pretty hard going. Can anyone recommend a good online source or tutorial to help me get my head around it?

Cheers
Dan
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Do you have the K&B scjp 1.6 book ?


SCJP 6. Learning more now.
Dan Webber
Greenhorn

Joined: Mar 31, 2011
Posts: 5
I'm using this one:

http://www.amazon.co.uk/gp/aw/d/0321556054/ref=mp_s_a_3?qid=1306747166&sr=8-3

It has been pretty good on the whole, but don't like the explanations it gives on generics.

Cheers
Dan
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Dan Webber wrote:I'm using this one:

http://www.amazon.co.uk/gp/aw/d/0321556054/ref=mp_s_a_3?qid=1306747166&sr=8-3

It has been pretty good on the whole, but don't like the explanations it gives on generics.

Cheers
Dan


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.

hth
bose
Dan Webber
Greenhorn

Joined: Mar 31, 2011
Posts: 5
Hmm, I suppose I trust you ;-)

Thanks for the advice, my mate has that book, so i'll have a read..

Cheers
Dan
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 40
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.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
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.


OCA, OCP Java 6
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I've always found the best way to learn about generics is to use them. Write lots of test programs.

For instance, a challenge you can set for yourself is to implement interesting data structures like Binary Search Trees, Heaps, or your own implementation of a List.

Implementing sorting and searching algorithms (even if they're very simple) can teach you a lot about generics. Remember that studying is only half the battle, you need to practice as well.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

http://download.oracle.com/javase/tutorial/java/generics/index.html

There you are


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Thanks guys.

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...
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 40
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good generics tutorial