Possible Duplicate:
Why don't Generics support primitive types?
I want to make one function for int, short and long. I have this in 3 almost identically function.
private <T extends Number > T scanTInteger() throws IOException, NumberFormatException
{
T number = 0;
But i don’t knew how to do generic type like int, short, long. For Object it is easy.
Is there is method for that.
EDIT
Ok i knew how to do that with out generic function. I will use only long and max size of primitive types and at the end convert it. If someone find how to do with generic i will be happy 🙂
You could create your own class which wrapped any one of these three types, and then handle them within that class’ code. You would not have any of the language-specific automated handling, autoboxing and the like, but that would give you what you appear to be asking for.
However, it seems to me the primary reason for making one function is to write some piece of logic one time instead of three. I suggest looking at having different methods for each data type, and then having those three methods each call one method that contains your logic. It can be (should be) private, and therefore hide whatever handling you do (before and/or after your central logic) to accommodate all three types, calling them will be easy from any point, since the primitives don’t have to be inserted into the wrapper, and having 3 one-line methods instead of one generic method is not very much trouble for either a maintainer or a user.
In other words, is the use of generics really worth it in your case? They’re really meant for use with classes, not primitives.