-
Dec 22nd, 2024, 06:29 PM
#1
Thread Starter
Frenzied Member
How do I pass any type into a function without conversion?
When I have an function where one of the parameters is of type Object (designed to allow passing of anything into the function), and I pass in a number like 123 (which is internally an object of type int), it automatically converts it to an object of type Integer (if you do getClass() on the parameter on the function it got passed into, it will give you a java.lang.Integer class, not an int class).
I want a way to have a parameter in my function that is generic enough to take any type and NOT convert it. If I pass an int, it should stay as an int. If I explicitly pass in an Integer (by creating an Integer object, and then pass that) the parameter should take it as an Integer. This may not seem important, but when the function that I'm trying to pass the parameter to, then does certain operations on that parameter (specifically using it with java.lang.reflection functions) it becomes important as to the EXACT type of the object. This is because the reflect functions depend on knowing the exact class of an object, and an int is a DIFFERENT class than an Integer. Even though usually they are treated the same in most of Java, they are technically different classes, and as such, when they are then used as inputs to reflection functions it becomes EXTREMELY important that an int stays an int, and doesn't get converted to Integer.
So when I have my function that then uses reflection functions, I need a way to pass into my function a very generic parameter, and doing "void myFunc(Object anything)" is just not enough. Because when I then call that function with myfunc(123), even though 123 is an int (not an Integer object), inside the myFunc function it is seen as an Integer object because I had passed it through a parameter of type Object instead of type int. But Object is the only way I know of to generically pass anything into a function. But the problem is it does in int to Integer conversion (I assume it would also do a double to Double conversion, or short to Short conversion, as well). I need to have a generic type of parameter that is even MORE generic than Object, so it keeps an int as an int, and keeps an Integer as an Integer, and keep a short as a short, and keep a Short as a Short.
In other words, I need to find a way to pass a parameter to my function that keeps primitive type values as primitives, and Object type values as Objects, and not do any conversion between primitives and Objects. Is this even possible in the Java language?
Last edited by Ben321; Dec 22nd, 2024 at 06:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|