A Funny Java Flavoured Look at the World

Friday, August 04, 2006

Generic Epiphany - an Angled bracket vision

The past few days I have been looking at a lot of Generic code, reading articles about generics, looking at more generics code, snacking on generic code and washing it down with a nice generic drink and finally going to bed and dreaming about ...yes of course Generics.

I have been reading all sorts of stupid examples about putting apples and pears into a fruitbasket and how the Doctors class is using generics to check the different animals but of course you can't just put a dog or elephant into an animal class oh no.

I have blogged about Generics a fair bit, here is the last effort and it goes back through the others if you want a generic frenzy of information

http://hoskinator.blogspot.com/2006/08/type-safe-collections-generics-arent.html

I have kept plugging away with Generics and I have a decent grasp of it but one thing that I always had to double check was the placment of the angled brackets. Even copying out examples to play around with the code, I would often put the angled brackets in the wrong place. Anyway I was typing in an example and then all of a sudden, the angled brackets suddenly made sense, just like that, boom. It was a like a magic eye picture had suddenly come into view or when you look at the picture of the young woman looking one way and then suddenly realise that it can also be an old woman (if you have seen the picture you know what I mean, if not you probably think I'm mad)

I understand the bit inside the angled brackets is the "type" but I often got slightly confused with the talk of the base type and then then the type but it's actually not as difficult as people talking about it seem to make out.

I have found it's often easy to look at Generic code and reverse it (mentally) to non generic code and then see what you have got.

so you have this

List<String> myList = new ArrayList<String>();

becomes

List myList = new ArrayList();

look at it this way helped me understand how Generics worked more clearly and simplified the angled bracket placing conundrum I had been experiencing. It also help me understand the base class they were talking about e.g. List and ArrayList (List is the Base class and ArrayList is the subclass).

Looking at it now it seems fairly obvious and probably is to a lot of people out there but I now understand the angled brackets need to go anywhere where you are mentioning a class/object you are going to use e.g. List, ArrayList etc. so now I see the above

so this

List<String> myList = new ArrayList<String>();

I now see

base class type Variable name = new subclass class type ()

but I find it helpful to take this code back to the non generic style to understand what is going on
List myList = new ArrayList();

Obviously this is an easy example and it can get very complex but here is another example

A method example taking in an ArrayList

public List<String> changeStrings(ArrayList<String> s) { }

to this:

public List changeStrings(ArrayList s) { }

Once again it seems easier to imagine the pre generic code show first and then adding it in to see what is actually happening in the code.

I just thought I would share my Generic breakthrough on the blog in case anyone else was having a few problems with Generics, it's definitly one of the tricky sections on the SCJP exam I think.

9 Comments:

  • I think you're missing something here - all your examples are the same.

    By Anonymous Anonymous, at Fri Aug 04, 09:16:00 am 2006  

  • thanks for the comment.

    Basically blogger was eating all the angled brackets thinking their were html and I had to do this

    <String>

    By Blogger The Hosk, at Fri Aug 04, 10:00:00 am 2006  

  • I had to use

    use & lt; and & gt;

    but with the & next to gt (I can't do it now because it will come out as an angled bracket as in the comment above

    By Blogger The Hosk, at Fri Aug 04, 10:02:00 am 2006  

  • Just escape the ampersand using &amp;: &lt;

    By Anonymous Anonymous, at Fri Aug 04, 10:16:00 am 2006  

  • List is not the base class of ArrayList.

    By Anonymous Anonymous, at Wed Aug 09, 03:30:00 am 2006  

  • Generics is basically stealing some of the functionality from C# and it works in a similar fashion.

    if you make an ArrayList<String> then the compiler checks to make sure you only add in Strings and that it returns strings so there is no casting.

    All the angled bracket code is removed by the JVM whilst compiling, so it is really only a compile time check.

    on a different note

    List is the base class of ArrayList isn't? Well it can't be used as a base class and then you can change to use any collection extending the list interface without changing any of the other code.

    By Blogger The Hosk, at Wed Aug 09, 08:55:00 am 2006  

  • Interesting article; it brings me to this: I had a similar difficulty wrapping my head around the specifics of generics until I realized that (like just about everything I have ever learned about programming) I was over-complicating what is essentially a simple concept.

    By Anonymous Anonymous, at Wed Aug 09, 04:04:00 pm 2006  

  • A simple look at the JavaDocs would should you what List is an interface, not a class.

    By Anonymous Anonymous, at Thu Aug 10, 02:23:00 am 2006  

  • well you are indeed right it isn't a class.

    I was thinking of the List interface as being the base class in a polymorphic type way rather than actually being a class not an interface.

    but I hold my hands up and admit you are absolutely correct and thanks for pointing out my rather ambiguous wording, especially on a topic that is already very confusing.

    Cheers

    Hosk

    By Blogger The Hosk, at Thu Aug 10, 08:51:00 am 2006  

Post a Comment

<< Home