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!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Java: Check if a String contains another Sub-String with Examples - Java
- How to Create a Website (Webpage) using HTML on Windows Notepad - Windows
- Upload a File in Microsoft SharePoint using PowerShell Script - SharePoint
- How to add multiple spaces between html page text - Html
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required. - MacOS
- How to save a file in Nano Editor and Exit - Linux
- Facebook : Warning: Request without access token missing application ID or client token - Facebook
- Fix: ModuleNotFoundError: No module named requests - Python