Forum Stats

  • 3,874,160 Users
  • 2,266,675 Discussions
  • 7,911,745 Comments

Discussions

is Generics in Java the same as Templates in C++

793927
793927 Member Posts: 61
edited Aug 28, 2002 12:20PM in Generics
Hi,
i am new to java, and i am wondering if generic classes the same as Templates in C++ ?
where can i get specific info from the site about how to use them ?
thanks

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I've never used C++, but I gather that the syntax is similar, but the main differences are i) GJ doesn't allow you to use primitive types, because they're not objects; ii) GJ only produces one chunk of code, rather than one per type of object used in the template.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    The answer is no. They address the same issue, and have similar looking syntax, but there are a lot of differences.

    Basically, C++ Templates sucked. They generated a lot of bogus code, were't as reusable as they could be, and many of the implementations in compilers were buggy.

    In typical Java fashion, Sun have cleaned up a lot of the problems, taken a much more dynamic and type-safe approach, while still using approximately the same syntax just to make C++ people feel comfortable.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    The answer is no. They address the same issue, and
    have similar looking syntax, but there are a lot of
    differences.

    Basically, C++ Templates sucked. They generated a lot
    of bogus code,
    Not quite sure what this means. There are implicit and explicit template generation. The code has to be generated somewhere, so the fact that it is generated can't be the cause for that comment. And since it only generated exactly what the programmer specified how would that be "bogus"?

    The reason that implicit vs explicit generation is needed has to do with linking and not with C++. Linking is outside of the domain of C++ but it still has to be dealt with by the compiler in some fashion.
    were't as reusable as they could be,
    How so? Mine certainly were.
    and many of the implementations in compilers were
    buggy.
    This has nothing to do with the idea itself. Templates in C++ are much more powerful than generics in Java. With power comes complexity. And that means bugs are more likely.

    And I seriously doubt that the first version of generics in java will have zero bugs.

    >
    In typical Java fashion, Sun have cleaned up a lot of
    the problems, taken a much more dynamic and type-safe
    approach, while still using approximately the same
    syntax just to make C++ people feel comfortable.
    I suspect more than making C++ people feel comfortable they faced the same problem that C++ language implementors did. One hopes that the syntax of the language is obvious and easily followed by the practitioners with out causing undo work on the compiler itself.

    This is why languages seldom have operators that are more than two characters long. Programers don't like them, and it makes compilers much harder to write and thus slower.

    And so when implementing a new feature one needs to choose a syntax that fits that. And for the contexts in use the selection of the angle brackets makes the most sense. It won't confuse the compiler (this is true even more in java than in C++) and it is 'obvious' to the developers what is going on when they see the syntax.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    The reason that implicit vs explicit generation is
    needed has to do with linking and not with C++.
    Linking is outside of the domain of C++ but it still
    has to be dealt with by the compiler in some
    fashion.
    You're right, but isn't that a failing of C++? Java can do implicit generation because it can do the dynamic linking. If generics ever turned up in languages like Objective-C or SmallTalk, they'd be fine with it as well (except that people seem to have a strange reverence for old languages, and are therefore unwilling to change them).

    Templates in C++ are much more powerful than generics
    in Java.
    How so?

    With power comes complexity. And that
    means bugs are more likely.

    And I seriously doubt that the first version of
    generics in java will have zero bugs.
    Granted, of course. I'm not claiming Java is perfect, but in deliberately limiting the power to those areas it's most needed they also limit the bugs.

    At risk of sounding extreme, think of all the 'power' that goto would give you, and the bugs it causes. Often, power is not the only consideration. In large, modern programs, you're far more concerned with provability and understanding of the structure.

    In typical Java fashion, Sun have cleaned up a lot of
    the problems, taken a much more dynamic and type-safe
    approach, while still using approximately the same
    syntax just to make C++ people feel comfortable.
    I suspect more than making C++ people feel comfortable
    they faced the same problem that C++ language
    implementors did. One hopes that the syntax of the
    language is obvious and easily followed by the
    practitioners with out causing undo work on the
    compiler itself.

    This is why languages seldom have operators that are
    more than two characters long. Programers don't like
    them, and it makes compilers much harder to write and
    thus slower.

    And so when implementing a new feature one needs to
    choose a syntax that fits that. And for the contexts
    in use the selection of the angle brackets makes the
    most sense. It won't confuse the compiler (this is
    true even more in java than in C++) and it is
    'obvious' to the developers what is going on when they
    see the syntax.
    Let's be clear on this point. Java's syntax is largely derived from C++. Given the choice between two syntaces that were equally powerful, expressive and typesafe, Sun will always use the one that feels more C++ish. Why? Because it makes their customers happy.

    By this point, though, that's no longer relevant. Java's generic syntax may have developed entirely separately from C++s, and simply converged on something that looks about the same without ever making a deliberate choice. But the reason why the same syntax makes sense is because the existing Java syntax is so similar to what the existing C++ syntax was when templates were being added to it.

    And before you start, I like the syntax in Java, more than I do Objective-C, SmallTalk or C++.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    You're right, but isn't that a failing of C++?
    No. It is just different.
    Templates in C++ are much more powerful than generics
    in Java.
    How so?
    They allow primitives. They allow default template parameters. Template syntax actually runs during the compile (mini-interpreter) which allows for some interesting library and coding building schemes. The whole area of template functions (vs only class templates.) I am not sure but can java do method templates (different from both function and class templates?)
    And I seriously doubt that the first version of
    generics in java will have zero bugs.
    Granted, of course. I'm not claiming Java is perfect, but in
    deliberately limiting the power to those areas it's most needed they
    also limit the bugs.
    So templates in C++ are more powerful?

    And if java did have as much power then we could expect the same number of bugs?

    My argument is not that power causes bugs. But rather that you can't say that the initial complexity with the implementations meant that C++ templates are 'less' than java's. I wouldn't disagree that the complexity seemed extreme and that in itself is likely to lead to problems.

    But so do multiple inheritance and operator overloading. And both of those had bugs in the early implementations. But it was the usage of the complexity and not the initial bugs that was at fault.
    Let's be clear on this point. Java's syntax is largely derived from C++.
    And my point is, is that if you took a black box approach with someone who did not know C++, then you be very likely to end up with exactly the same syntax.

    So, by using C++ as a starting point, all it did was save time. It wasn't done to make the C++ people happy. (If decisions were made that way then operator overloading and multiple inheritance would be part of the language.)



This discussion has been closed.