Swaminathan K
asked on
Unable to create a generic method in java using Generics
Hi Team,
Iam creating an Generic Class which can be used to add any type of numerical values in java like below. Iam creating an generic method which add the two numerical values . I can pass any object type like Integer, Float , Double , Short or Long types . Any help is really appreciated.
package Generics;
public class ArithematicOperations<T extends Number> {
private T t1;
private T t2;
public ArithematicOperations(T t1 , T t2)
{
this.t1=t1;
this.t2=t2;
}
// add two numbers , the input can be of any type
public <T extends Number> T addNumbers()
{
// getting error here
return (T)t1 + t2;
}
public static void main(String [] args)
{
ArithematicOperations<Integer> ao = new ArithematicOperations<Integer>(10,20);
}
}
Iam creating an Generic Class which can be used to add any type of numerical values in java like below. Iam creating an generic method which add the two numerical values . I can pass any object type like Integer, Float , Double , Short or Long types . Any help is really appreciated.
package Generics;
public class ArithematicOperations<T extends Number> {
private T t1;
private T t2;
public ArithematicOperations(T t1 , T t2)
{
this.t1=t1;
this.t2=t2;
}
// add two numbers , the input can be of any type
public <T extends Number> T addNumbers()
{
// getting error here
return (T)t1 + t2;
}
public static void main(String [] args)
{
ArithematicOperations<Integer> ao = new ArithematicOperations<Integer>(10,20);
}
}
The + operator is only valid for primitive types. It will not work on objects.
?
In the context of the numeric types (not counting auto-unboxing)
Some objects, such as String, can be concatenated using the + operator.
But now I believe I am getting a bit off-topic.
Some objects, such as String, can be concatenated using the + operator.
But now I believe I am getting a bit off-topic.
Integer i = 1;
Double d = 2.0;
Short s = 3;
System.out.println(i+d+s);
Yeah, that's auto-unboxing those objects into primitives, then applying the + operator to the primitives (and widening them, as necessary)
Yup I get that.
However you said
However you said
The + operator is only valid for primitive types. It will not work on objects.It’s not that what’s going on is not what you describe, but it’s not reflected in the statement.
Fair. It was overly general, and not entirely correct.
public class ArithematicOperations<T extends Number> {
private T t1;
private T t2;
public ArithematicOperations(T t1 , T t2)
{
this.t1=t1;
this.t2=t2;
}
// add two numbers , the input can be of any type
public <T> Number addNumbers()
{
// getting error here
return t1.intValue() + t2.intValue();
}
public static void main(String [] args)
{
ArithematicOperations<Integer> ao = new ArithematicOperations<Integer>(10,20);
System.out.println(ao.addNumbers());
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
awesome thanks a lot . Sorry for the late closing of the question
Thanks. As long as it fits the bill - some might say it’s highly artificial such an approach. ;)
I should add that there is a nasty side-effect to the ternary operator use in my code, because due to numeric promotion, a Double is returned, not an Integer. To remove this effect, you'd have to resort to using if - else structure when returning the result, like this :
(Of course the value of the calculation wouldn't be wrong when using the ternary code, only its type, and the lexical representation of the result - so it would appear as 30.0 instead of 30).
if((t1.toString()+(t2.toString())).contains(".")){return da;}
return ia;
//return (((t1.toString())+(t2.toString())).contains("."))?ia:da;
. . . so don't use the commented-out line. (Of course the value of the calculation wouldn't be wrong when using the ternary code, only its type, and the lexical representation of the result - so it would appear as 30.0 instead of 30).
But the Number interface does not have any method defined for addition, so if you want to add two numbers, the Number interface may not be appropriate for you.