To find Integers (or primitive int datatype) max and min values you can print use of the Integer wrapper class Integer.MAX_VALUE and Integer.MIN_VALUE constant values.
You can see that these are defined as static final values as hexadecimal values in java.lang.Integer class.
/**
* A constant holding the minimum value an int can have, -231.
*/
@Native
public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an int can have, 231-1.
*/
@Native
public static final int MAX_VALUE = 0x7fffffff;
Example:
package org.code2care.java.examples;
public class Main {
public static void main(String[] args) {
int intMaxValue = Integer.MAX_VALUE;
int intMinValue = Integer.MIN_VALUE;
System.out.println("int/Integer Min Value in Java: " + intMinValue);
System.out.println("int/Integer Max Value in Java: " + intMaxValue);
}
}
Output:
Caution with Integer Overflow and Underflow issues
Be very careful when working with them, as you can end up with overflow and underflow issues which are hard to debug at times.
Overflow Example:
int intMaxValuePlusOne = Integer.MAX_VALUE + 1;
System.out.println(Integer.MAX_VALUE +" + 1 = "+ intMaxValuePlusOne);
int intMaxValuePlusOne = Integer.MAX_VALUE + 1;
System.out.println(Integer.MAX_VALUE +" + 1 = "+ intMaxValuePlusOne);
As you can see when I just add 1 to the integers max value, we get an unexpected answer. This is called overflow.
Underflow Example:
int intMinValuePlusOne = Integer.MIN_VALUE + 1;
System.out.println(Integer.MAX_VALUE +" - 1 = "+ intMinValuePlusOne);
int intMinValuePlusOne = Integer.MIN_VALUE + 1;
System.out.println(Integer.MAX_VALUE +" - 1 = "+ intMinValuePlusOne);
As you can see when I just subtract 1 from the integer's min value, we get an unexpected answer. This is called underflow.
To avoid make sure you know the range of the values you are dealing with, or make use of subtractExact() and addExact() static methods form java.math package that was added in Java 8 to deal with underflow and overflow issues.
Example:
Integer intMinValueMinusOne = Math.subtractExact(Integer.MIN_VALUE, 1);
System.out.println(Integer.MIN_VALUE +" - 1 = "+ intMinValueMinusOne);
That's better! You can simply add a try-catch block and handle it accordingly.
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- How to Make a Windows Notepad File Read-Only - Windows
- Tool To Add Watermark to Images (100% free, no login required) - Tools
- Create Nested Directories using mkdir Command - Linux
- How to access SharePoint on Office 365 using a Web Browser - SharePoint
- Python String contains substring equivalent example - Python
- How to Add Brew to PATH M1/M2 Mac - MacOS
- Which Python version is bundled with macOS Sonoma 14 by default - MacOS
- Java Thread.sleep() Method Deep Dive with Examples - Java