Re: Java vs C++, A Newbie's perspective.



Timo Stamm <timo.stamm@xxxxxxxx> wrote:
On a very abstract level, they both serve the same purpose of generic
programmming. As Bjarne Stroustrup said: "we need to parameterize our
containers with the element type and do operations on parameterized
containers." That's what Java Generics do.

To me, it looks as if they only serve the same purpose on a NOT very
abstract level. Ultimately, generic collections of reference types in
Java are possible not because of generics, but because of the existence
of a universal supertype for all reference types. Generic collections
or primitive data are not possible because Java doesn't provide a
universal supertype for primitive data (neglecting boxing conversions),
and generics don't change that.

It's important for Java programmers to understand that generics in Java
are a type system feature. They do not in any way relate to what the
code does! Java's generics define a somewhat intricate language for
expressing syntactic constraints on situations in which certain code is
appropriate. Once it is determined that the code is appropriate, the
code is executed exactly as if it had not been written with generics at
all. Generics allow the programmer to limit the range of uses of some
piece of code, and then rely on the information added by these
constraints later on.

This is a huge difference from C++ templates, the whole purpose of which
is to write templates for code, which are expanded to different code
under different uses. While some of the possible expansions of
templates may differ from each other only in type checking, there are
others that differ from each other in terms of static resolution of
method calls, or even (with template specialization) completely
different structures of code. At the same time, C++ templates do NOT
provide any fundamentally new vocabulary for expressing type
constraints. Every pure type constraint on code using C++ templates is
a type constraint from the non-templated subset of the C++ language
after expansion of the templates. (I'm neglecting the type constraints
that are defined on non-type template parameters, as in
"template<int a>", because they are outside the scope of the discussion
here.) Unlike Java, in which generics augment the subtype relation with
wildcard types that allow more flexible typing of expressions, C++
templates don't introduce any type relationships that wouldn't otherwise
be there.

Clearly, it's possible to find similarities between these two features,
but I certainly wouldn't call them abstract. Instead, the similarities
seem to be that both features are used in the context of collection
classes.

--
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
.



Relevant Pages

  • Re: Why Generics?
    ... > Now I haven't used generics in Java (I'm leaning heavily towards FORBIDDING ... In other words, all of your concerns about templates in C++ are valid, ... criticize it. ...
    (comp.lang.java.programmer)
  • Re: Why Generics?
    ... Robert Maas, see http://tinyurl.com/uh3t wrote: In Java5, the powers that be added generics. ... Can someone please explain to me what value they add to Java? ... from one class to another, diddling tiny changes each time, I started ... thinking how nice C++ style templates would be. ...
    (comp.lang.java.programmer)
  • Re: Java vs C++, A Newbies perspective.
    ... Templates are much more powerful than Java Generics. ... These are almost entirely unrelated purposes. ...
    (comp.lang.java.programmer)
  • Re: java going in the wrong direction (1.5)
    ... I have seen some truly evil templates in C++, but the Java generics do ... Enums, similarly, are something that are needlessly complex in current ...
    (comp.lang.java.programmer)
  • Re: Java Generics: limitations?
    ... cannot go around and do something similar to what I've said, then Java ... generics cannot be called generics, ... have pointed out, the HasXY interface is not necessary, and could be ... public int getX(); ...
    (comp.lang.java.programmer)