3

My question is: is it possible to make one generic method where I can use 2 different types? E.g. an Integer and a String.

There is no practical use for this, but I'd just like to know if it's possible. And if it is, how? :)

6
  • 4
    What part of the methods would the generic type affect (arguments, return value, something else)? Commented Nov 8, 2012 at 9:48
  • do you mean something like V put(K key, V value) in java.util.Map Commented Nov 8, 2012 at 9:50
  • Yes, it is possible. Take a look into java.util.Map Commented Nov 8, 2012 at 9:50
  • There are several ways to do this. Which is most suitable depends on the context. Commented Nov 8, 2012 at 9:51
  • 1
    Do you mean that you want a method such as <T> foo(T t), where T can be either a String or an Integer? Commented Nov 8, 2012 at 10:10

4 Answers 4

2

You dont need to use generics for this. You can overload the methods. For example

public void method(Integer i){}

public void method(String s){}

If you call method with an integer then it will call the first method. If you call it with a string it will call the second method.

Sign up to request clarification or add additional context in comments.

4 Comments

@brimborium, I do not think its missing the point. It could sovle the problem. For those specific types. I think that this perspective is good one.
@Vash OP stated that he thinks there is no practical use for this. He just wants to know if it is possible using generics.
@brimborium there is no (practical or useful) way to do that with generics. Using overloaded methods is the best way (IMO) to do stuff like this. The OP or anybody looking for an answer might not be aware of this so it's worth pointing it out, so I'd say this answer is quite on target!
@GiovanniBotta Ok, so the answer should have been: "No, but here is a different approach for it: [...]". If somebody asks you how to catch a fish with your bare hands, you don't explain to him how to do it with a fishing rod. At least not before telling how he would do it with his bare hands and why that is a bad/ineffective/expensive/timeconsuming/whatever/suboptimal approach.
1

Presumably you mean two different types of parameter?

You could use one method for the String param, and another than takes the integer and passes it on to the String version.

Comments

1

The example types are not good one as the are final. This mean that you can not use them to limit the generic parameter as nothing can inherit from them.

So for that types the answer is No.

But what we can do with Java is:

You can create a method that accept Object type.

 public <T> void doStaff(T obj) {

 }

You can create a method that is limited to CharSequence as String is final

 public <T extends CharSequence> void doStaff(T str){ 

 } 

You can create a method that is litmited to more the one interface

public <T extends CharSequence & Comparable<T>> void doStaf(T interf) {

}

But even with so many possibilities we can not create a generic parameter that is valid for Two unrelated types. This would not have sense. As main task of generic type is to provide type safety. And in generally when we use them we should operate with interfaces not classes.

6 Comments

I don't think the last example is valid Java code. What would it do? How can you work with a type that is either of type One or Two? Only an & makes really sense here...
As far as I can tell, Comparable seems to be an interface, so it is not extended, it's implemented ;-)
@tehlexx, yeah that was w typo cause lack of morning coffe fiex after posting.
@santirivera92, Interfaces can be extended by interfaces and implemented by classes.
@santirivera92, Serializable or Comparable<T> are ofter inherited ;-).
|
0

If you want your method to accept Integer and String, try below approach:

  public class Test {
public static void main(String[] a)  {
    method(new String(),new Integer("2"));
}
public static <T extends String, U extends Integer> void  method(T t, U u){

}
}

EDIT:

if you want your method to take a single parameter that takes either a String or Integer

try this :

public static <T extends Comparable<T>> void  method(T t){

}

12 Comments

You can not extends from String or Integer, their are final Type.
@Vash although you extending String or integer is not possible, it works in the case of generics. the above code would compile. :)
Waht JVM you are using ? For sure on 5 and 6 will not compile.
@Vash did you try compiling the code ?? and my JVM version is 1.6.0_23
@Vash it would give you the below warning, not error ,.. The type parameter U should not be bounded by the final type Integer. Final types cannot be further extended - The type parameter T should not be bounded by the final type String. Final types cannot be further extended
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.