3

For example the Math.max( .. .. ) method is overloaded to support different types of Number. One name for all of them because, no mather the type of Number, it does the same thing.
So does the parseNumberType methods defined for each class extending Number.

Why is it the way it is ? Wouldn't have been better if a parameterized parse method was defined in Number ( and Number of course, parameterized ) like : public abstract T parse( String s ); ( and implemented specifically afterwards in all the subclasses of Number )

  • 2
    Remember that Java 1.0 didn't have generics. They came in Java 5. But even then, parseXxx returns a primitive, not an object. – JB Nizet Jul 17 '15 at 08:05

3 Answers3

2

The various versions of Math.max have primitive arguments, not Numbers (which are Objects). What you suggest would require a boxing/unboxing operation each time the method is called which would not be as efficient as the current design.

assylias
  • 297,541
  • 71
  • 621
  • 741
2

Number exists since JDK 1.0, Generics were only introduced in Java 1.5. Such a generic parsing method would therefore not been possible.

Concerning Math.max, it accepts only primitive types, which do not have any form of relationship to each other, and every primitive type needs to be declared separately to be supported.

wonderb0lt
  • 1,922
  • 1
  • 20
  • 32
0

Parse instance methods would be pointless. You'd have to construct the immutable value before the parse.

Tom Hawtin - tackline
  • 139,906
  • 30
  • 206
  • 293