Why does Java have so many analysis methods?

For example, the Math.max (....) method is overloaded to support different Number types . One name for all of them, because, don't print the Number type , it does the same.
Thus, parseNumberType methods are defined for each class that extends Number .

Why is it so? It would not be better if the parameterized parsing method was defined in Number (and Number of course, parameterized) like:    public abstract T parse( String s );

(and implemented specifically after that in all subclasses of Number)

+3


source to share


3 answers


Number

has been around since JDK 1.0, Generics were only introduced in Java 1.5. Thus, such a general method of parsing would not be possible.



Relatively, Math.max

it only accepts primitive types that have no relationship with each other, and each primitive type must be declared separately for support.

+2


source


The various versions Math.max

have primitive arguments, not numbers (which are objects). What you are suggesting will require a boxing / unboxing operation on every method call, which will not be as efficient as the current design.



+2


source


Parse instance methods would be pointless. You will need to construct an immutable value before parsing.

0


source







All Articles