TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Integer.MAX VALUE, MIN and SIZE

Examine the MIN_VALUE and MAX_VALUE final ints on Integer and other classes. Use loop boundaries.
Max, integer. All numeric types have a maximum and minimum value they can represent. Only so many bytes are available—this limits numbers.
Notes, final ints. In Java the numeric classes, like Byte and Integer, have public final fields (constants). These tell us properties of the number types themselves.
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.

TYPE: This returns a class reference. This matches the shortened type, like int for Integer.

SIZE: This returns the numbers of bits in the type. There are 8 bits in a byte.

MIN_VALUE: This is the smallest value the numeric class can represent. All the types except Char have sign bits.

MAX_VALUE: This is the highest value possible in a type. For Double and Long, these are huge.

Java program that shows type information 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); } } Output 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
Loop boundaries. 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.

Caution: We must use "less than" Integer.MAX_VALUE not <= because otherwise the loop index will overflow.

Java program that uses Integer.MAX_VALUE, loops 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"); } } Output DONE
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.
Java program that uses byte 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); } } Output 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.
Java program that uses short 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); } } Output 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.
Java program that uses long 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); } } Output 100000000000000000
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.

Java program that uses toBinaryString 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); } } } Output 0 1 10 11 100 101 110 111 1000 1001
Notes, numbers. 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.DoubleFloat
A summary. 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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf