Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics methods in Java

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? :)

like image 807
StevenVerheyen Avatar asked Jul 17 '26 22:07

StevenVerheyen


1 Answers

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.

like image 192
RNJ Avatar answered Jul 20 '26 10:07

RNJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!