GenericCalculator<T extends Number>
is a Java class that provides basic arithmetic operations (addition, subtraction, multiplication, and division) for numeric types using Java Generics. It allows performing calculations on different number types (Integer
, Double
, Float
, etc.) while maintaining type safety.
- Supports arithmetic operations: addition, subtraction, multiplication, and division.
- Works with any type that extends
Number
. - Uses
doubleValue()
to ensure precision in calculations. - Handles division by zero with an exception.
Since T
extends Number
, Java provides methods like intValue()
, doubleValue()
, floatValue()
, etc. However, using T
directly in operations like T + T
is not possible because Java Generics do not support primitive arithmetic. Instead, all operations are performed using doubleValue()
, ensuring consistent precision and avoiding type mismatch errors.
- Generics do not support primitive types →
T
cannot be used directly in arithmetic. doubleValue()
provides a common numeric format that can represent allNumber
subtypes.- Ensures type safety and avoids casting issues when performing calculations.
- Returning
Integer
would lose precision, as floating-point calculations might be needed. - Java Generics do not allow instantiation of
T
, making it impossible to create newT
objects for returning values directly.
While returning Integer
could be achieved via explicit casting, it may introduce rounding errors and potential ClassCastException
. Using Double
ensures safe and precise calculations for all numeric types.
Java generics do not support primitive types, meaning you can't perform direct arithmetic like T + T
.
public T add(T other) {
return value + other; // ❌ Compilation Error
}
Instead, doubleValue()
is used as a common numeric format for safe calculations:
public Double add(T other) {
return value.doubleValue() + other.doubleValue(); // ✅ Works for all Number types
}
Java provides multiple numeric conversions (intValue()
, floatValue()
, etc.), but doubleValue()
is the most flexible.
public Double multiply(T other) {
return value.doubleValue() * other.doubleValue();
}
Using doubleValue()
ensures calculations remain precise and work for all Number
types.
If the method returned Integer
, it would truncate decimal values, causing precision loss in calculations.
public Integer add(T other) {
return (int) (value.doubleValue() + other.doubleValue()); // ❌ Loses precision
}
Example:
GenericCalculator<Double> doubleCalc = new GenericCalculator<>(5.5);
System.out.println(doubleCalc.add(1.2)); // Expected: 6.7, but would return 6
Java does not allow new T()
due to type erasure.
public T add(T other) {
return new T(value.doubleValue() + other.doubleValue()); // ❌ Compilation Error
}
Since the exact type of T
is unknown at runtime, using doubleValue()
ensures a valid return type.
If you need the result to be the same type as T
, you must manually cast it:
public T add(T other) {
Number result = value.doubleValue() + other.doubleValue();
return (T) result; // ⚠️ Unchecked cast (may fail for certain types)
}
However, this is unsafe and may cause ClassCastException
at runtime.
public static void main(String[] args) {
GenericCalculator<Integer> intCalc = new GenericCalculator<>(10);
GenericCalculator<Double> doubleCalc = new GenericCalculator<>(5.5);
System.out.println("Integer Operations:");
System.out.println("10 + 2 = " + intCalc.add(2));
System.out.println("10 - 2 = " + intCalc.subtract(2));
System.out.println("10 * 2 = " + intCalc.multiply(2));
System.out.println("10 / 2 = " + intCalc.divide(2));
System.out.println("\nDouble Operations:");
System.out.println("5.5 + 1.5 = " + doubleCalc.add(1.5));
System.out.println("5.5 - 1.5 = " + doubleCalc.subtract(1.5));
System.out.println("5.5 * 1.5 = " + doubleCalc.multiply(1.5));
System.out.println("5.5 / 1.5 = " + doubleCalc.divide(1.5));
}
- Division by Zero:
This prevents undefined operations when dividing by zero.
if (other.doubleValue() == 0) { throw new ArithmeticException("Division by zero is not allowed."); }
- All calculations return
Double
, even when usingInteger
orFloat
, which may lead to slight precision loss in certain cases. - Does not support
BigDecimal
orBigInteger
.
The GenericCalculator
class is a flexible solution for performing arithmetic operations on different numeric types. By using doubleValue()
, it ensures that all calculations are performed safely and consistently, avoiding issues related to Java's type system restrictions in Generics.