Lesson: The Number Classes

Explore Java classes for basic numeric data types.

Understand what the Number classes are and how they are used, including the concepts of boxing and unboxing.


We explored Java's primitive data types in an earlier lesson. While we use primitive numeric data types directly most of the time, there are times when we need to treat a numeric primitive as an object. For this reason Java provides the Number Classes. There is a subclass of Number for each numeric primitive:

  • Byte (byte)
  • Integer (int)
  • Short (short)
  • Long (long)
  • Double (double)
  • Float (float)

These classes wrap the primitive data type in an object. Often the compiler wraps the primitive for you. If you use an primitive where an object is expected, the compiler "boxes" the primitive in its wrapper class for you. If you use a Number object where a primitive is expected the compile will convert or "unbox" the object into the primitive value.

There are three main reasons why you would want to use a Number object instead of the corresponding primitive type:

  • As an argument of a method that expects an object.
  • To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  • To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

The last item is the most common reason.  The Number classes provide many methods for converting, parsing and otherwise manipulating numbers. Many of the methods are static, meaning you can use them without an actual instance of a Number class. A typical use is converting (parsing) a string of digits into a Number object or the primitive type. The various methods can be quite useful.

You can also use instances of Number classes just like you would a primitive type. Here is an example showing an Integer variable being used just like a primitive int and also showing one of the static methods of Integer to convert a String to an integer value:

Lets look at what is happening in the example. First we create an Integer object and set it's value to 3. Then we perform a math operation on that Integer object. To do this, Java unboxes the Integer object i to a primitive integer and adds 2 to it. The statement returns the primitive integer value 5 and Java sees that we want to put a primitive 5 into the Integer object i and so Java boxes the primitive integer back into an Integer object, which now contains 5 as it's value.

Next we print out the value of the Integer object i. Java sees we are using i like a primitive value and so unboxes the Integer object to a primitive value which println knows how to handle. We also see that since i is an Integer object, we can use the methods defined for the Integer object, one of which is toString() which returns a string representation of the numeric value. The println function sees a string and knows how to handle it.

Next we use a static method of the Integer class, parsetInt(n), to parse a string of digits into an int value and store that int value into a primitive int variable. You can see that the methods of the Integer object can be used to work with both primitive int values and Integer objects.

Finally we print the value of the primitive int j. Again, println knows how to handle primitive int values so the first j is no problem. However, the next display of j tries to use the toString() method. This will fail since the variable j is a primitive int, which does not have methods.

Here is this example on CodingGround. Compile to see the error generated by Java when you try to use a method on a primitive data type variable. Fix the problem and get the program to run.

Here is a detailed discussion of the Number Classes.


Lesson navigation: