A Funny Java Flavoured Look at the World

Wednesday, August 02, 2006

Type Safe Collections (Generics) aren't so bad

I was reading some stuff about Generics and Collections and doing some boring study for SCJP 5 Exam and passing this exam really is the carrot at the end of the tunnel. I have read a number of blog entries about Generics and complaining that it's the opposite of Generic and that it's infact prohibative. Although I agree with this in some way but it doesn't really bother me and in fact I think that Generics works fine and if you create an List then it should only be able to accept JavaClass unless you wanna put a wild card ? and allow subclasses etc.

Although it is adding extra complexity and catches you out the first few times in the long run I think it makes it easier to understand because you can tell what is going to be in the Collection.


which I think is brilliant and have reviewed it here

Anyway I read this linke

"Arrays in Java have always been type safe—an array declared as type String (String[]) can't accept Integers (or ints), Dogs, or anything other than Strings. But remember that before Java 5 there was no syntax for declaring a type safe collection"

It's funny but I had never thought about arrays being type safe but they are. I think that any move to catch more bugs in compile time and making Collections type safe is just to catch any code which tries to put the wrong type of object into a Collection is a good thing.

I think Generics will be a sort of problem area for me in the exam because we don't use Java 5 yet at work so the only coding I have done with it is just mucking about. I have been writing small examples but it would be good if we could use Generics and Java 5 at work, fistly because it would give us some type safe collections and secondly because then I could practise writing code using Generics.

I have blogged about Generics before


and if you want some resources to learn about Generics and Collections, then look no further I have a blog entry with some links for you


and if that lots doesn't send you to sleep I don't know what will.

0 Comments:

Post a Comment

<< Home