This discussion is archived
3 Replies Latest reply: Sep 20, 2010 4:19 AM by gimbal2 RSS

need help in learning generic

807580 Newbie
Currently Being Moderated
Hi,
I am studying for the SCJP exam. I am almost done, at chapter 14, reading the book A Programmer's Guide to Java SCJP Certification by Mughal and Rasmussen. This chapter on generic is the most difficult for me yet. I think I have a superfical grasp rule of usage of generic in Java. However, I really don't know the reason these rules of usage is founded upon. All I can see now on the topic is a large set of rules that I need to memorize without much intuition. Can anyone point me to a direction, book or other resources, so I can gain a firmer footing on the topic? Thank you.
  • 1. Re: need help in learning generic
    jduprez Pro
    Currently Being Moderated
    Sun published a "Generics Tutorial for Java Programming Language" [EDIT: by Gilad Bracha indeed, thanks Penguin], search for it.

    From the top of my head, the two most driving reasons I can think of for this set of rules are:
    - set algebra may defeat intuitive understanding (in general, a set of cats is not a set of animals; but if you only read from it, it is).
    - retrofitting generics into (at that time, non-generic) collection classes had to be upward compatible (code compiled prior to generics had to work unchanged when executed by a 1.5 VM)

    With that in mind, I'm certainly far from remembering the whole set of rules, so I would suck at such a certification exam, but I'm surfacely aware of enough traps that it never hampered my work at any place with drinking water and a decent compiler.

    Edited by: jduprez on Sep 20, 2010 10:49 AM
  • 2. Re: need help in learning generic
    807580 Newbie
    Currently Being Moderated
    O'Reilly Java Generics and Collections, Naftalin & Wadler

    And anything Gilad Bracha has ever written, ever (Even if its not about Generics).
  • 3. Re: need help in learning generic
    gimbal2 Guru
    Currently Being Moderated
    Incidentally, I did the SCJP exam not too long ago and the questions I got about generics were the basic usage and declaration ones, no "trap" questions. In fact in the entire exam the only real 'trap' question I got were the two I expected to get:
    boolean b = false;
    
    if(b = true){
     System.out.println("YES!");
    } else {
     System.out.println("NO!");
    }
    and
    int a = 5;
    
    if(a = 10){
     // yada
    }
    The remainder of the questions were fair tests of knowledge. Difficult, but quite fair. Of course I could have been just lucky.