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 source code shows that Number is mostly an empty shell, unlike, for 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 than being a common ancestor of other numeric types with no real functionality?

5 Answers 5

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

3 Comments

safely? ;-) Not sure about that.
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.
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.
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.