Java – why does the number class not have methods such as add () or close ()?
So this is a question about Java design
Why Java There are no add () and negate () methods in the lang. number class, but what about some subclasses?
I mean... There is no unity I can be in float, long and others auto@R_442_2419 @Using the or operator on the easy class, I can use add() and close() in the case of BigDecimal or BigInteger (which violates SRP)
So if we allow these operations on byte / short / integer / long (with auto boxing and operators), why not just add an abstract number () and
Is there a reason?
Solution
The most important thing is to maintain backward compatibility
The methods you mentioned are not in Java In the first version of lang. number, they need to be abstract Why abstract? Suppose you create your own subclass of ComplexNumber, then it has a negative () method, and now a negative () method is added to number If the negate () method is not abstract, then in Java Lang. number should have a general implementation of it What type of object should be returned? It is impossible to make a good decision If it is a double, like this: public number negative() {return new double (this. Doublevalue());}? This will suddenly convert your ComplexNumber to a double negative value
And adding an abstract method will not work because it will destroy the existing third-party subclass of number, so it will destroy backward compatibility
Backward compatibility is the reason why it seems obvious that many possible API improvements cannot be achieved