Re: Java vs C++, A Newbie's perspective.
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Mon, 3 Jul 2006 16:54:03 -0600
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
.
- References:
- Java vs C++, A Newbie's perspective.
- From: fiziwig
- Re: Java vs C++, A Newbie's perspective.
- From: Moiristo
- Re: Java vs C++, A Newbie's perspective.
- From: Timo Stamm
- Re: Java vs C++, A Newbie's perspective.
- From: Moiristo
- Re: Java vs C++, A Newbie's perspective.
- From: Timo Stamm
- Re: Java vs C++, A Newbie's perspective.
- From: Chris Smith
- Re: Java vs C++, A Newbie's perspective.
- From: Timo Stamm
- Java vs C++, A Newbie's perspective.
- Prev by Date: Need help
- Next by Date: keytool questions
- Previous by thread: Re: Java vs C++, A Newbie's perspective.
- Next by thread: Re: Java vs C++, A Newbie's perspective.
- Index(es):
Relevant Pages
|