What happened to Generic Programming in Java?

H

Hung Jung Lu

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
 
V

VisionSet

Hung Jung Lu said:
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?
 
H

Hung Jung Lu

VisionSet said:
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
 
T

Thomas Schodt

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.
 
N

Neal Gafter

You can get beta-quality previews of the next release, which includes generics
among other new language features. To sign up, send email to
(e-mail address removed), 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top