We’re going to take a deeper look at the Java

Number class. In this tutorial, we’ll focus on the Java Number constants available in

the Java Integer and Float classes. There’s many Number classes, we saw that in

the Autoboxing and Unboxing tutorial. For this tutorial, we’re going to focus on the

Integer and Float classes. That keeps the material simpler. The functionality available

in Integer and Float classes is also available in the other Java Number classes. Let’s start with the five constants in the

Integer class. The integer class has a minimum and maximum value. The constants are MAX_VALUE

and MIN_VALUE. We can remember this usually as 2 to the minus 32, and 2 to the plus 32

– 1, but we don’t want to calculate the power every time we need to use it. If we need the

actual value, that’s what these constants are for. They represent the maximum and minimum

values for the primitive type. We’d use these in programs where we need to

protect against edge cases. For example if we’re looking for the smallest number in a

list, we might set the initial value to the largest. We know every value in the list should

be smaller than or equal to the MAX_VALUE. When we run the loop and look at every number

in the list, we have an initial value to compare against. If we’re looking for the largest

number, we just start with the smallest number and look for numbers bigger. The BYTES and SIZE constants tell us how large

is the data type. When working with integers, on some machines we’ll have a 32 bit number

and others a 16 bit number. It depends on the CPU, version of Java, and so on. For most

personal computers, BYTES will return 4 and SIZE will return 32 for Integers. The Long

class would return 8 and 64 respectively. The last constant is the class type called

TYPE. Class objects are meta objects, and TYPE simply returns a class describing the

primitive class wrapped by the enclosing class. Integer also has a method called getClass().

getClass() returns a class describing the wrapper class Integer. The distinction is

getClass() is returning the meta class for the wrapper class Integer, while TYPE is getting

the meta class for the primitive type int. If we print the class name, getClass() prints

“java.lang.Integer”, while TYPE.toString prints “int”. The reasons for the distinction are important

when we get to Reflection in Java. That’s a ways off, but the idea is if we ask what

the class type is for a variable, the Integer class is different from the int primitive.

If we just know we have a Number instance, we’ll use the TYPE constant to determine what

kind of primitive the Number instance is wrapping. The Float class has 7 more constants for a

total of 11 constants. The MAX_VALUE and MIN_VALUE constants return the maximum and minimum numbers

represented with a float. Again these constants are useful in programs where you need to set

edges for your numbers. BYTES and SIZE also return the primitive size

for the machine. And TYPE still returns a class describing the Float class. These all

work the same way for floats. If you remember from the Java floating-point

number intricacies lesson, a floating point number is separated into different bit fields.

These bit fields represent the sign, exponent, and mantissa. Java provides constants for

the maximum and minimum exponent. For most machines MAX_EXPONENT for floats is 127, and

MIN_EXPONENT is -128. One example of where these are used are in

the Java math library. The scalb method increases the exponent of a number. It’s like using

the pow method or shifting the number. If the resulting exponent is larger than MAX_EXPONENT,

it’s too large for the float, so the number gets set to infinity. The scalb method is

using MAX_EXPONENT for another type of boundary testing. MIN_NORMAL is a bit more complicated. So the smallest number we can represent looks

like this. The exponent is 1 minus the bias 127, which equals -126. The mantissa is all

zeros, and using the implied bit, we get 1.0 x 2-126. If the exponent is all zeros, the implied

mantissa bit is also zero. When the implied bit is zero, the mantissa is called denormalized.

In this case, the smallest number that you can represent changes. The exponent is 0 minus

the bias 127, which equals -127. And the last bit is the smallest bit we can use, since

the number is now normalized. The smallest positive float is 1.0 X 2-149. That’s a huge

difference in the smallest number representable. This is used to get more numbers near zero

at the expense of accuracy. The last three constants for the Float class

are all about representing impossible numbers. That’s positive infinity, negative infinity,

and not a number. These constants are the IEEE representation for the float primitive.

The numbers are returned by some trigonometric and logarithmic functions. If you have any questions, add them to the

comments below. Next we’ll look at Java number conversions. Liking the video helps me know

what I’m doing right, so if you liked the video… you know what to do. Don’t forget

to subscribe to the DeegeU newsletter. In each newsletter, I’ll send out a summary of

the videos for that month and tips and news not included in the videos! I’ll see you in the next tutorial!