- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
The Integer class of Java provides two constants named Integer.MAX_VALUE and Integer.MIN_VALUE represents the maximum and minimum possible values for an integer variable in Java. The actual value of Integer.MAX_VALUE is 231 -1 which is equivalent to 2147483647 and the actual value of Integer.MIN_VALUE is -231 which is equivalent to -2147483648. For our convenience, instead of writing this big whole number in our program, we use their constant representation. This article aims to explore the utility and how to use Integer.MAX_VALUE and Integer.MIN_VALUE in our Java programs.
Examples of Integer.MAX_VALUE and Integer.MIN_VALUE in Java
In this section, we are going to see the different ways of using Integer.MAX_VALUE and Integer.MIN_VALUE in our Java programs.
Example 1
The following example shows us how to use the Integer.MIN_VALUE and Integer.MAX_VALUE to display the maximum and minimum possible values for an integer variable in Java.
public class Example1 { public static void main(String[] args) { // storing the minimum and maximum values int smallest = Integer.MIN_VALUE; int largest = Integer.MAX_VALUE; // to show the smallest and largest integer values System.out.println("The smallest value is: " + smallest); System.out.println("The largest value is: " + largest); } }
Output
The smallest value is: -2147483648 The largest value is: 2147483647
Example 2
The following example illustrates how to find the largest and smallest elements from an array using Integer.MIN_VALUE and Integer.MAX_VALUE.
Approach
The first step is to declare and initialize an array with 5 integer elements.
Now, store the minimum and maximum values to integer variables.
Define a for-each loop that iterates through the defined array.
Inside this loop, take two if blocks to compare and update the smallest and largest elements from the given array.
In the end, print the results and exit.
public class Example2 { public static void main(String[] args) { // initializing an array of integers int[] numericList = {10, -5, 3, 7, 15}; // storing the minimum and maximum values int smlst = Integer.MAX_VALUE; int lrgst = Integer.MIN_VALUE; // to loop through the given array for (int number : numericList) { // to compare each element with smallest and largest if (number < smlst) { // to update smallest if a smaller element is found smlst = number; } if (number > lrgst) { // to update largest if a larger element is found lrgst = number; } } // to show the smallest and largest element from array System.out.println("The smallest element is: " + smlst); System.out.println("The largest element is: " + lrgst); } }
Output
The smallest element is: -5 The largest element is: 15
Overflow condition of Integer.MAX_VALUE and Integer.MIN_VALUE
An overflow condition occurs when the result of an operation exceeds the range of the data type. If we try to add 1 to Integer.MAX_VALUE, we will get Integer.MIN_VALUE as the result. Similarly, if we try to subtract 1 from Integer.MIN_VALUE, we will get Integer.MAX_VALUE as the result.
Example 3
In this example, we will show the practical implementation of Overflow condition of the Integer.MAX_VALUE and Integer.MIN_VALUE constants.
public class Example3 { public static void main(String[] args) { // storing the minimum and maximum values int smallest = Integer.MIN_VALUE; int largest = Integer.MAX_VALUE; // to show the smallest and largest integer values System.out.println("The value after subtracting 1 from Integer.MIN_VALUE is: " + (smallest - 1)); System.out.println("The value after adding 1 to Integer.MAX_VALUE is: " + (largest + 1)); } }
Output
The value after subtracting 1 from Integer.MIN_VALUE is: 2147483647 The value after adding 1 to Integer.MAX_VALUE is: -2147483648
Conclusion
These two constants Integer.MAX_VALUE and Integer.MIN_VALUE comes very handy while performing various operations such as finding the largest and smallest elements from a collection of integers. In this article, we have learned the ways of using these two constants through example programs.