2

I have seen questions like Java Generics and adding numbers together frequently and the answer usually boils down to the fact that you can't really do anything with Number itself.

The Sourcecode shows that Number is mostly an empty shell unlike for an example Object.

At this point I can't think of a situation where using Number instead of a concrete subtype has significant (if any) advantages. Usually I start out with Number but run into problems later on actually using it and change it to a subtype anyway. But this may just be my limited experience.

So I am wondering, what is the purpose of this class other then being a common ancestor of other numeric types with no real functionality?

Community
  • 1
  • 1
Stefan
  • 838
  • 3
  • 13
  • 28

5 Answers5

3

As you can see in the Javadocs, it's an abstract class. By definition, then, its sole purpose is to serve as the base class for other numeric types. There is a set of methods (e.g., intValue()) which every Number subclass will have, and one can imagine writing something like

if (object instanceof Number)
    result = ((Number) object).intValue();
else if (object instanceof String)
    result = Integer.parseInt(object.toString());

I think that's about it. What else would you expect?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
2

It allows for the following:

  1. Perform (possibly lossy) conversion from one type to another.
  2. Allows client code to be written to use a more precise form (long or double) while storing the smaller values.
  3. Allows intermediate code to pass numbers without being too specific (Long) or too abstract (Object).
  4. Sort of allows code to scale from fixed precision (Integer) to infinite precision (BigInteger)... but to do this right, they should have added the arithmetic operations to the base class.
  5. Allow client code to consume values without paying attention to mutability (Long vs. AtomicLong), provided it does not store references to the objects.
  6. Allow alternative implementations for representations of numbers, for example writing your own Fraction that can be expressed as a double.
  7. Allows for cursor-style APIs, for example an IntegerCursor. But keep in mind that when using this technique, you need to be sure the client code will not store references to the number.
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
0

Because all Java numeric types extends Number, and Number provides conversion methods to all other numeric formats, Java programmers can be always sure that the can safely convert from one numeric type to another. This is a very useful property.

Renato
  • 12,940
  • 3
  • 54
  • 85
  • safely? ;-) Not sure about that. – Ustaman Sangat Dec 29 '11 at 18:40
  • Well, as safely as possible... at least you won't have Exceptions thrown! But of course, if you try to convert carelessly without checking ranges or considering whether truncation and rounding might be a problem, then you will have trouble for sure. – Renato Dec 29 '11 at 19:03
  • 2
    If you really want to do it safely, store the value as a BigDecimal and then use the various exact methods (e.g., BigDecimal#longValueExact). Those will throw an exception if the size of the desired primitive cannot store the value properly. – Saish Dec 29 '11 at 22:51
0

An abstract class extended by all classes that represent numeric primitive types such as Byte, Short, Integer, Long, Float, and Double. The class contains abstract methods that convert the object value to any of the other numeric types. All these methods are overridden by the numeric subclasses. It also contains methods to convert numeric types to strings and vise versa. See

public abstract class java.lang.Number extends java.lang.Object implements java.io.Serializable
{
     //......
}

See a long example that uses the Number class

Lion
  • 18,729
  • 22
  • 80
  • 110
0

Well I wished it at least implemented Comparable but I do see how Integer.compareTo(SomeNumberType) may not concur with SomeNumberType.compareTo(Integer); and that's why only the subclasses implement Comparable.

Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26