views:

75

answers:

4

So say I have 2 methods: one adding two int's and the other adding two long's. Where the parameters are passed into the function.

How can I make 1 method that generalizes the parameter and return value to perform this?

+1  A: 

http://en.wikipedia.org/wiki/Generics_in_Java

DVK
+3  A: 

Unfortunately, I don't think you can do this. First, Java generics can't be parameterized over primitives, so you'd have to use Integer and Long instead of int and long. Second, while you could have a method with generic type parameter T extends Number, there's no way to add two arbitrary Number subclasses in a generic fashion. In other words, the following code will not work:

public static <T extends Number> T add(T a, T b) {
    return a + b; // Won't compile, as the addition is not legal Java code.
}
bcat
Why the downvote? If I'm mistaken, and there is a way to do this, I'd really like to know how.
bcat
@bcat: Was writing my comment about why I downvoted when you made that comment. Anyway... Your first sentence is quite misleading. It's easy to use the wrapper classes for primitives, and your latter point is probably a nonissue because Brandon's existing methods are for adding two ints and two longs, which implies that he wants the generic method to have that same functionality and not necessarily be able to add an int and a long.
JAB
Yes. Only for adding two ints and two longs.
Brandon
@JAB: Brandon specifically asked about ints and longs, not Integers and Longs. While the difference is often unimportant due to autoboxing, it still exists, and it's a good thing to be aware of. I do agree that he doesn't want to be able to add an `int` and `long`. I think he's asking how to write a single method that takes two parameters, either both ints or both longs, adds them, and returns a value of the same type as the two parameters. As far as I know, this is impossible in Java without manual type checks and casts, at which point his two method approach is probably better.
bcat
Yes. I'm looking for a single method.
Brandon
A: 
long f(long x, long y){ ... }

int x=1, y=2;
f(x,y);
irreputable
This doesn't do exactly what the OP wants, since `int z = f(x,y)` won't compile without casting the return value of `f` to `int`.
bcat
+2  A: 

I saw a trick like this in GWT:

public static <T> T add(long a, long b)
{
    return (T)(Object)(a + b); 
}

Use it like this:

long answer = add(1L, 2L);
int answer2 = add(1, 2);

While this does what you're asking for, the downside of this approach is that it opens you up to bad type casts. Even though you don't have to explicitly cast the value that is returned, you're still effectively telling the code to perform a cast to whatever the expected type is. You could accidentally say something like:

String t = add(1, 2);

Which would cause a runtime exception. Nevertheless, if you have a method whose return value will always be cast immediately after being returned, this trick can save consumers the hassle of explicitly casting the value.

StriplingWarrior
Hm... when I run this, I have to declare the variables as Integer and Long. Compilation fails when I use primitives. And I get a runtime exception for a failed Integer<->Long cast.
JAB
Interesting. I didn't actually try compiling and running this, but Eclipse wasn't complaining so I assumed it would at least compile.
StriplingWarrior