What happened to Generic Programming in Java?

Discussion in 'Java' started by Hung Jung Lu, Nov 11, 2003.

  1. Hung Jung Lu

    Hung Jung Lu Guest

    Hi,

    It's well-known that C++ has template classes, Java does not.

    A few years ago there was a movement to incorporate Generic
    Programming to Java. What happened?

    What approaches are people using for Generic Programming? By Generic
    Programming I mean the design of code that is applicable to any class.
    For instance, at some point I'd like to do

    y = x.a + x.b

    without caring a priori what class x belongs to. In C++, you use
    templates. In Java, I can think of using three ways of do generic
    programming: (a) interfaces, (b) reflection, and (c) some indirect way
    via usage of collection. But is there any other well-known approach in
    Java for Generic Programming?

    regards,

    Hung Jung
     
    Hung Jung Lu, Nov 11, 2003
    #1
    1. Advertising

  2. Hung Jung Lu

    VisionSet Guest

    "Hung Jung Lu" <> wrote in message
    news:...
    > Hi,
    >
    > It's well-known that C++ has template classes, Java does not.
    >
    > A few years ago there was a movement to incorporate Generic
    > Programming to Java. What happened?
    >
    > What approaches are people using for Generic Programming? By Generic
    > Programming I mean the design of code that is applicable to any class.
    > For instance, at some point I'd like to do
    >
    > y = x.a + x.b
    >
    > without caring a priori what class x belongs to. In C++, you use
    > templates. In Java, I can think of using three ways of do generic
    > programming: (a) interfaces, (b) reflection, and (c) some indirect way
    > via usage of collection. But is there any other well-known approach in
    > Java for Generic Programming?
    >


    public abstract class Generic {

    abstract void specific1();
    abstract void specific2();

    public void generalise() {

    // do generic stuff
    specific1();
    specific2();
    }

    public class Specific extends Generic {

    public void special() {
    generalise();
    }

    void specific1() { } // do specific1 stuff
    void specific2() { } // do specific2 stuff
    }

    I think this is refered to as the template pattern, is this the sort of
    thing you mean?

    --
    Mike W
     
    VisionSet, Nov 11, 2003
    #2
    1. Advertising

  3. Hung Jung Lu

    secret Guest

    they're coming out in 1.5 which should be released shortly.


    "Hung Jung Lu" <> wrote in message
    news:...
    > Hi,
    >
    > It's well-known that C++ has template classes, Java does not.
    >
    > A few years ago there was a movement to incorporate Generic
    > Programming to Java. What happened?
    >
    > What approaches are people using for Generic Programming? By Generic
    > Programming I mean the design of code that is applicable to any class.
    > For instance, at some point I'd like to do
    >
    > y = x.a + x.b
    >
    > without caring a priori what class x belongs to. In C++, you use
    > templates. In Java, I can think of using three ways of do generic
    > programming: (a) interfaces, (b) reflection, and (c) some indirect way
    > via usage of collection. But is there any other well-known approach in
    > Java for Generic Programming?
    >
    > regards,
    >
    > Hung Jung
     
    secret, Nov 12, 2003
    #3
  4. Hung Jung Lu

    Hung Jung Lu Guest

    "VisionSet" <> wrote in message news:<G8esb.448$>...
    >
    > public abstract class Generic {
    > ...
    > }
    >
    > public class Specific extends Generic {
    > ...
    > }
    >
    > I think this is refered to as the template pattern, is this the sort of
    > thing you mean?


    Well, kind of, but not exactly. Generic Programming is an old and
    well-established concept/approach. For instance, in the example I
    gave:

    y = x.a + x.b

    the variables could be real, integer, or strings for concatenation. A
    typical example is the macro implementation of MAX(x, y) in C++, where
    x and y could be integer, double, or anything that allow magnitude
    comparison. You want to write the code only once, in a
    type-non-specific way, and let the compiler/runtime take care of
    generating the type-safe code. (Here "type" includes the meaning of
    "class", not just basic types.) C++ generates the type-safe
    realizations at compile time, C# verifies the type safety at compile
    time but generates the type-safe realizations at runtime.

    For weakly-typed languages like Python or Ruby, Generic Programming is
    an automatic feature. Because there is type checking at each step
    during run time, weakly-typed languages by nature are much slower.
    (There are hacks to boost performance, but I am talking in general.)

    Generic Programming is not as innocent and simple as it may seem. It
    opens the door to a wider set of re-usable code. Java boasts
    reflection, interfaces, etc., but C++ templates in some areas are
    still much more powerful/usable. That is why Java is incorporating
    Generic Programming, now. I'd say Java is 3 years late in the process,
    though.

    Hung Jung
     
    Hung Jung Lu, Nov 12, 2003
    #4
  5. Seems to me there is a fine line between generics and obfuscation.

    I always assumed that was the reasoning behind Java not having
    the C++ style operator overloading.
     
    Thomas Schodt, Nov 12, 2003
    #5
  6. Hung Jung Lu

    nos Guest

    did you forget the semi?
    y = x.a + x.b
    no wonder it is a problem

    "Thomas Schodt" <news0310@xenoc.$DEMON.co.uk> wrote in message
    news:Xns943170CD361A2xenoc@158.152.254.254...
    > Seems to me there is a fine line between generics and obfuscation.
    >
    > I always assumed that was the reasoning behind Java not having
    > the C++ style operator overloading.
     
    nos, Nov 12, 2003
    #6
  7. Hung Jung Lu

    Neal Gafter Guest

    How to get pre-beta copies of J2SE 1.5.0 (was What happened to GenericProgramming in Java?)

    You can get beta-quality previews of the next release, which includes generics
    among other new language features. To sign up, send email to
    , asking to be added to the CAP program. Tell her Neal Gafter
    sent you to sign up. She will send you a nondisclosure form to sign and FAX
    back. Once she gets it back from you you'll get access to a web site that has a
    new alpha of the entire J2SE platform (solaris, linux, and windows) every two
    weeks. Including a working javadoc.

    Also see <http://forum.java.sun.com/forum.jsp?forum=316>

    Regards,
    Neal

    Hung Jung Lu wrote:
    > Hi,
    >
    > It's well-known that C++ has template classes, Java does not.
    >
    > A few years ago there was a movement to incorporate Generic
    > Programming to Java. What happened?
    >
    > What approaches are people using for Generic Programming? By Generic
    > Programming I mean the design of code that is applicable to any class.
    > For instance, at some point I'd like to do
    >
    > y = x.a + x.b
    >
    > without caring a priori what class x belongs to. In C++, you use
    > templates. In Java, I can think of using three ways of do generic
    > programming: (a) interfaces, (b) reflection, and (c) some indirect way
    > via usage of collection. But is there any other well-known approach in
    > Java for Generic Programming?
    >
    > regards,
    >
    > Hung Jung
     
    Neal Gafter, Nov 18, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul O'Grady
    Replies:
    19
    Views:
    726
    Patricia Shanahan
    Feb 18, 2005
  2. Murat Tasan
    Replies:
    1
    Views:
    8,081
    Chaitanya
    Feb 3, 2009
  3. Replies:
    5
    Views:
    6,353
    jan V
    Sep 3, 2005
  4. Replies:
    2
    Views:
    448
  5. minlearn
    Replies:
    2
    Views:
    466
    red floyd
    Mar 13, 2009
Loading...

Share This Page