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);
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);
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.
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- How to reload Zsh .zshrc Profile - zsh
- Install OpenSSL on Linux/Ubuntu - Linux
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse
- Mac Shortcut for Fullscreen mode for App Window - MacOS
- Java Join Strings with Comma Separator - Java
- How to add Date and Time to Windows Notepad File - NotepadPlusPlus
- How to submit website to dmoz directory - HowTos
- Java + Spring JDBC Template + Gradle Example - Java