Everyone knows how simple arithmetic in Java works for primitives:
int one = 1,two = 2,three = one + two;
double four = (one + two) / three;
if(four % 1 == 0){
System.out.println("Yay for simple maths!");
} else{
throw new RealityError("http://www.youtube.com/watch?v=H91rPIq2mN4");
}
But the moment that you leave primitives things become complicated fast! Attempting to repeat the previous simple arithmetic is quite the challenge!
BigInteger bigOne = BigInteger.ONE, bigTwo = BigInteger.valueOf(2),
bigThree = bigOne.add(bigTwo);
BigDecimal bigFour = new BigDecimal(bigThree.divide(bigOne.add(bigTwo)));
if (BigInteger.valueOf(bigFour.longValueExact()).mod(BigInteger.ONE).equals(BigInteger.ZERO)) {
System.out.println("Yay for simple maths?!?!");
} else {
throw new RealityError("http://www.youtube.com/watch?v=H91rPIq2mN4");
}
Why aren't annotations used for operators like +, -, *, or /
? Such as @DefaultAddOperator
for a class that extends Number. Or, in the BigInteger class, why can't it implement an interface that simply states what operation to do when an operator is called on the class. This notion isn't too far fetched, I mean it's used with "Hello World!".toString()
and the overloaded operator +
, and would simplify the above class based arithmetic considerably.