Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Good generics tutorial

 
Dan Webber
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the K&B scjp 1.6 book ?
 
Dan Webber
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/java/generics/index.html

There you are
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic