Thread: Templates in Java

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Templates in Java

    The upcoming Java 5.0 (1.5.0) has support for templates. They are different from the templates in C++ though, but they look quite similar.

    http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

    I think this is a great addition to the/any language, whether you like it or not (let's not start a flame war on Java).

    I've skimmed though the article and have one question:
    Code:
    //Templated classes are written like this:
    public interface Test<T>
    {
      ...
    }
    //But templated methods are written like this
    public <T> int test(T t)
    {
      ...
    }
    Why not test<T>?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why not test<T>?
    I would wager because it's more difficult to parse for methods.
    My best code is written with the delete key.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Sang-drax
    Why not test<T>?
    then how would you declare the parameter 't'? youd be left with:
    public int test<T> (T t) ... or something....

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Perspective
    then how would you declare the parameter 't'? youd be left with:
    public int test<T> (T t) ... or something....
    Yes, that would be consistent.

    Or if that is too hard to implement for some reason, change the class declaration:
    Code:
    public <T> interface Test{
      ...
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Sang-drax
    Or if that is too hard to implement for some reason, change the class declaration:
    Code:
    public <T> interface Test{
      ...
    }
    yeah, i like that suggestion better.... now, if only java was open source (as it should be) these things would have been avoided early on in the development stages; damn Sun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM