The Numbers Classes
By dananourie on Jan 07, 2009
Working with numbers can be a large part of application programming. Follow this tutorial to get familiar with the Numbers classes, and how to work with numbers in your applications.
When working with numbers, most of the time you use the primitive types in your code. For example:There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes "wrap" the primitive in an object. Often, the wrapping is done by the compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you. Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you.int i = 500; float gpa = 3.65f; byte mask = 0xff;
Here is an example of boxing and unboxing:WhenInteger x, y; x = 12; y = 15; System.out.println(x+y);
yare assigned integer values, the compiler boxes the integers because
yare integer objects. In the
yare unboxed so that they can be added as integers.
All of the numeric wrapper classes are subclasses of the abstract class
There are three reasons that you might use a
Note: There are four other subclasses of
Numberthat are not discussed here.
BigIntegerare used for high-precision calculations.
AtomicLongare used for multi-threaded applications.
Numberobject rather than a primitive:
- As an argument of a method that expects an object (often used when manipulating collections of numbers).
- To use constants defined by the class, such as
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 following table lists the instance methods that all the subclasses of the
Methods Implemented by all Subclasses of Number Method Description
Converts the value of this
Numberobject to the primitive data type returned.
int compareTo(Byte anotherByte)
int compareTo(Double anotherDouble)
int compareTo(Float anotherFloat)
int compareTo(Integer anotherInteger)
int compareTo(Long anotherLong)
int compareTo(Short anotherShort)
Numberobject to the argument.
boolean equals(Object obj)
Determines whether this number object is equal to the argument.
The methods return
trueif the argument is not
nulland is an object of the same type and with the same numeric value.
There are some extra requirements for
Floatobjects that are described in the Java API documentation.
Numberclass contains other methods that are useful for converting numbers to and from strings and for converting between number systems. The following table lists these methods in the
Integerclass. Methods for the other
Numbersubclasses are similar:
static Integer decode(String s)
Decodes a string into an integer. Can accept string representations of decimal, octal, or hexadecimal numbers as input.
static int parseInt(String s)
Returns an integer (decimal only).
static int parseInt(String s, int radix)
Returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (
radixequals 10, 2, 8, or 16 respectively) numbers as input.
Stringobject representing the value of this
static String toString(int i)
Stringobject representing the specified integer.
static Integer valueOf(int i)
Integerobject holding the value of the specified primitive.
static Integer valueOf(String s)
Integerobject holding the value of the specified string representation.
static Integer valueOf(String s, int radix)
Integerobject holding the integer value of the specified string representation, parsed with the value of radix. For example, if s = "333" and radix = 8, the method returns the base-ten integer equivalent of the octal number 333.