Forum Stats

  • 3,853,788 Users
  • 2,264,269 Discussions
  • 7,905,444 Comments

Discussions

i don't see a reason why we need generics.

843793
843793 Member Posts: 41,732 Green Ribbon
edited Oct 10, 2002 9:57AM in Generics
i downloaded the current specification of generics in java and noticed that they do not work with primitive datatypes, only with Objects. so iam wondering why generics?

Generics which do only work with Objects is so stupid! all i can do with them is what i could also do with polyphormism! adding generics means to introduce a lot of bugs, incompatibility, making the language much more complicated (the compiler must get more complicated, more documents, the programmer need more knowledge).

SO WHY????? i do not see a reasonable reason for that!!!
«1

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    This question has been posed several times before in this forum. Basically generics add a significant amount of additional compile time type safety which can only be achieved currently by adding extra classes (with error prone repetitive keying of definitions). I would guess that at least 80% of typical commercial code would benefit from generics over objects and have little or no need for generics over primitive types.
    While I would like to see generics extended to primitives as well it is more difficult and could still be done at a later stage. The current generics proposal provides excellent compatibility with existing code.
    Finally yes the programmer will need to know a bit more, but to paraphrase a great man, if you are tired of learning you are tired of life.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I would guess that at least 80% of typical commercial code would benefit from generics over objects
    I agree.
    and have little or no need for generics over primitive types.
    Really? I seem to find myself forced to use wrappers quite a bit with GJ.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    and have little or no need for generics over
    primitive types.

    Really? I seem to find myself forced to use wrappers
    quite a bit with GJ.
    But would generics over primitives help or is auto boxing the solution for your wrappers? Note that I distinguish between allowing generics over primitives and allowing primitives in an object context (such as a Collection). It is one thing to allow a specific type parameter to be replaced by any (numeric) primitive (but not an object type), and quite another to allow unrestrained use of primitives.
    To allow unrestrained use of primitives you either need to go for auto boxing/unboxing (as in C#) or you have to eliminate the distinction between objects and primitives as in SmallTalk. The runtime implications of these alternatives are quite different. There is a hint (in one of the JavaOne papers) that auto boxing may also appear in JDK 1.5 (Tiger), and it could certainly work well in combination with generics.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Basically generics add a significant
    amount of additional compile time
    i think so too.
    I would guess that at least 80% of typical commercial
    code would benefit from generics over objects and have
    little or no need for generics over primitive types.
    why will they benefit? and from what? generics will only restrict the flexibility and gain nothing!

    for example it was possible to add Objects, Integers and Strings in one and the same Collection and extract them later with instanceof tests and there where now problems. you could also sort the whole collection with different kinds of objects stored in it(as long as they implement Comparable interface). with generics you don't have this flexibility anymore.

    in c++, templates (a kind of generics) were very useful because they cold used together with operator overloading to program each algorithm unbeatable flexible and efficient. this will never be possible in java so what?
    Finally yes the programmer will need to know a bit
    more, but to paraphrase a great man, if you are tired
    of learning you are tired of life.
    100% ACK! i will have no problems with it but others will. some things planned for the java 1.5 tiger released sounds very interesting (foreach loops, typesafe enums, covariant return types, etc.) but generics? why?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    But would generics over primitives help or is auto boxing the solution for your wrappers?
    Syntactically, I regard both as "generics over primitive types". I'd prefer though to have primitives as objects.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    why will they benefit? and from what? generics will
    only restrict the flexibility and gain nothing!
    The gain is not in performance but in maintainability. You can still use the original collection classes for heteorogenous collections if you wish, but if you have a Map which will always have a String key and Integer values, then you can declare it as such. For Map's I usually document the expected types of the keys and values, but inevitably sometimes the documentation gets out of sync with reality. If I used generics this wouldn't happen.

    in c++, templates (a kind of generics) were very
    useful because they cold used together with operator
    overloading to program each algorithm unbeatable
    flexible and efficient. this will never be possible in
    java so what?
    Although the initial implementation is only concerned with type safety, it might become possible in later versions for the JIT to benefit from knowledge of generic type information. So 'never' is by no means certain.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Although the initial implementation is only concerned
    with type safety, it might become possible in later
    versions for the JIT to benefit from knowledge of
    generic type information. So 'never' is by no means
    certain.
    assumed i have an Vector of EventListeners.
    Vector<EventListener> myVec = new Vector<EventListener>()
    but i can now pass all kinds of listeners in the Vector (all what is derived from EventListener). how could the jit optimize that?

    and boxing and unboxing is handled compiler intern the same way as wrapper classes and will not have any performance gain.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    for example it was possible to add Objects, Integers
    and Strings in one and the same Collection and extract
    them later with instanceof tests and there where now
    problems. you could also sort the whole collection
    with different kinds of objects stored in it(as long
    as they implement Comparable interface). with generics
    you don't have this flexibility anymore.
    That capability is not being removed from the language. A Collection<Object> does just that. It is up to the programmer to decide what set of types the containers he is using should contain and what if any value there is in narrowing their acceptance of types of objects in each case.

    Chuck
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    assumed i have an Vector of EventListeners.
    Vector<EventListener> myVec = new
    Vector<EventListener>()
    but i can now pass all kinds of listeners in the
    Vector (all what is derived from EventListener). how
    could the jit optimize that?
    As EventListener is just a tagging interface there would be little or no opportunity to optimise. However a compiler might be able to optimise some uses of Vector<WindowListener> or Vector<String>.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    so if i compare a Vector to Vector<String> the only difference is in the vector without generics i can add all object types and in the generics string vector i can only add Strings? is this the only difference what generics make???

    or have i possibilities with generics which are not possible without them? or are there some other advantages which justify the efforts resulting in adding generics to java?
This discussion has been closed.