Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

templated classes

  Asked By: Clifton    Date: Jul 11    Category: Java    Views: 603
  

What I'm looking for is the ability in Java to do something like the
following code example from C++.

template <class T>
T square(T number)
{
return number * number;
}

If we do the following:
int x = 2;
int i = 4;
i = square(x);
then the compiler will change the template to:

int square(int number)
{
return number * number;
}
and i will now be 4.

If we do the following:
float x = 2.3;
float int i = 4.2;
i = square(x);
then the compiler will change the template to:

float square(float number)
{
return number * number;
}
and i will now be 5.29.

Using templated methods in Java, the ArrayList toString section of
methods would be eight times smaller. But, it seems that, according
to java.sun.com/.../
and the section on new generic types on that page that this will still
not be possible. It seems that I'll still have to tell the compiler
ahead of time what variable type to expect, and thus I'll still have
to write eight different methods in order to handle being given any
one of the eight primitive variable types.

Any comments on this in relation to Tiger and Java in general? Have I
been supposing correctly/incorrectly?

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on templated classes Or get search suggestion and latest updates.




Tagged: