What are the Java Number Class Constants – J046

by | May 16, 2016




Transcript – Java Number Class Constants

We’re going to take a deeper look at the Java Number class constants. 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.

Java Number Class Constant for Integer

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.

public final static int findSmallest() {
    int[] array = { 3, 5, 7, 2, 9, 10 };
    int smallestValue = Integer.MAX_VALUE;
    for (int currentInt : array) {
       if (currentInt < smallestValue) {
           smallestValue = currentInt;
       }
    }
    return smallestValue;
}

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.

System.out.println("Size = " + Integer.SIZE);
System.out.println("Byte = " + Integer.BYTES);

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.

Integer i = -42;
System.out.println("class = " + i.getClass().toString());
System.out.println("pclass = " + Integer.TYPE.toString());

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.

Java Number Class Constants for Float

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.

Java Number Class Constant MIN_NORMAL

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.

Java Number Class Constant Conclusion

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!




Related Posts

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Daily Beetle by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Source: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1500025
Artist: http://incompetech.com/

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don't miss another video!


New videos come out every week. Make sure you subscribe!



Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This