Integer Max, Min and Size
This page was last reviewed on Apr 28, 2023.
Dot Net Perls
Max, integer. All numeric types have a maximum and minimum value they can represent. Only so many bytes are available—this limits numbers.
In Java the numeric classes, like Byte and Integer, have public final fields (constants). These tell us properties of the number types themselves.
Loop boundaries. To start, MAX_VALUE and MIN are helpful in loops. Sometimes you may want a high number—the maximum constant is convenient.
And To loop through the entire range of positive ints, we can use a for-loop and stop at MAX_VALUE.
Warning We must use "less than" Integer.MAX_VALUE because otherwise the loop index will overflow.
public class Program { public static void main(String[] args) { // Loop over all positive integers. for (int value = 0; value < Integer.MAX_VALUE; value++) { // ... Do something important. } // Loop over all positive integers in reverse. for (int value = Integer.MAX_VALUE - 1; value >= 0; value--) { // ... Do another important thing. } System.out.println("DONE"); } }
Type, size, min and max. This program is repetitive (sorry about that) but it serves its purpose. It displays the TYPE code, the SIZE and the min and max values.
Info Type returns a class reference. This matches the shortened type, like int for Integer.
Next Size returns the numbers of bits in the type. There are 8 bits in a byte.
Then Min_value is the smallest value the numeric class can represent. All the types except Char have sign bits.
Finally Max_value is the highest value possible in a type. For Double and Long, these are huge.
public class Program { public static void main(String[] args) { // Display common numeric types. System.out.println(Byte.TYPE); System.out.println(Byte.SIZE); System.out.println(Byte.MIN_VALUE); System.out.println(Byte.MAX_VALUE); System.out.println(Short.TYPE); System.out.println(Short.SIZE); System.out.println(Short.MIN_VALUE); System.out.println(Short.MAX_VALUE); System.out.println(Character.TYPE); System.out.println(Character.SIZE); System.out.println((int) Character.MIN_VALUE); System.out.println((int) Character.MAX_VALUE); System.out.println(Integer.TYPE); System.out.println(Integer.SIZE); System.out.println(Integer.MIN_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(Float.TYPE); System.out.println(Float.SIZE); System.out.println(Float.MIN_VALUE); System.out.println(Float.MAX_VALUE); System.out.println(Double.TYPE); System.out.println(Double.SIZE); System.out.println(Double.MIN_VALUE); System.out.println(Double.MAX_VALUE); System.out.println(Long.TYPE); System.out.println(Long.SIZE); System.out.println(Long.MIN_VALUE); System.out.println(Long.MAX_VALUE); } }
byte 8 -128 127 short 16 -32768 32767 char 16 0 65535 int 32 -2147483648 2147483647 float 32 1.4E-45 3.4028235E38 double 64 4.9E-324 1.7976931348623157E308 long 64 -9223372036854775808 9223372036854775807
Byte. The byte uses 8 bits of storage. In Java the byte is a signed number, so it can be negative or positive. We can cast larger values to bytes, but they will overflow.
public class Program { public static void main(String[] args) { byte value = 100; System.out.println(value); // Try to cast a large number to a byte. // ... The result may be unexpected. value = (byte) 9999; System.out.println(value); } }
100 15
Short. This is a two-byte integral type. It has a sign bit, so it supports negative and positive numbers. We use short to save memory, and to improve compatibility with existing software.
public class Program { public static void main(String[] args) { // Use a short local variable type. short number = 10; System.out.println(number); // Short has less range than int. System.out.println(Short.MIN_VALUE); System.out.println(Short.MAX_VALUE); } }
10 -32768 32767
Long. This is an 8-byte integral number type, twice the size of an int. This program creates a long from a value that cannot be stored in an int—it is too large.
public class Program { public static void main(String[] args) { // This number is too long to be an int, but it fits in a long. String value = "100000000000000000"; long result = Long.parseLong(value); System.out.println(result); } }
ToBinaryString. This method returns a String representation of an integer's bits. The leading zeros are removed (you can mentally add them on the left of the output).
Here We see how the bits change in the numbers 0 through 9. ToBinaryString helps us visualize bitwise operations.
public class Program { public static void main(String[] args) { // Convert 1 to 10 to binary Strings. for (int i = 0; i < 10; i++) { String result = Integer.toBinaryString(i); System.out.println(result); } } }
0 1 10 11 100 101 110 111 1000 1001
Numeric types in Java are self-descriptive, but some details are important. It is helpful to know the classes have public final numbers that tell us about their aspects.
For most programs, Integer (int) is best. But to save memory, Byte is better. And to store large numbers, or floating point ones, Long, Double and Float are best.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 28, 2023 (edit).
© 2007-2024 Sam Allen.