Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. Integer.MAX_VALUE / MIN_VALUE / Integer.toBinaryString()

Integer.MAX_VALUE / MIN_VALUE / Integer.toBinaryString()

Constants representing the maximum and minimum values that integer types (int and long) can hold. They are commonly used to check for overflow or to set initial values. Utility methods for converting integers to binary or hexadecimal strings are also provided.

Syntax

// Gets the maximum value of int (2147483647).
Integer.MAX_VALUE;

// Gets the minimum value of int (-2147483648).
Integer.MIN_VALUE;

// Converts an int value to a binary string.
Integer.toBinaryString(intValue);

// Converts an int value to a hexadecimal string.
Integer.toHexString(intValue);

// Converts an int value to an octal string.
Integer.toOctalString(intValue);

Constants and Methods

Constant / MethodDescription
Integer.MAX_VALUEA constant representing the maximum value of int: 2147483647 (231-1).
Integer.MIN_VALUEA constant representing the minimum value of int: -2147483648 (-231).
Long.MAX_VALUEA constant representing the maximum value of long: 9223372036854775807 (263-1).
toBinaryString(int i)Converts an int value to its unsigned binary string representation and returns it.
toHexString(int i)Converts an int value to its unsigned hexadecimal string representation and returns it (lowercase).

Sample Code

// Check the maximum and minimum values.
System.out.println(Integer.MAX_VALUE); // Prints "2147483647".
System.out.println(Integer.MIN_VALUE); // Prints "-2147483648".
System.out.println(Long.MAX_VALUE);    // Prints "9223372036854775807".

// Overflow check (MAX_VALUE + 1 wraps around to MIN_VALUE).
int overflow = Integer.MAX_VALUE + 1;
System.out.println(overflow); // Prints "-2147483648".

// Convert to binary.
System.out.println(Integer.toBinaryString(10));  // Prints "1010".
System.out.println(Integer.toBinaryString(255)); // Prints "11111111".

// Convert to hexadecimal.
System.out.println(Integer.toHexString(255)); // Prints "ff".
System.out.println(Integer.toHexString(256)); // Prints "100".

// Convert to octal.
System.out.println(Integer.toOctalString(8)); // Prints "10".

// Use MAX_VALUE as an initial value to find the minimum in an array.
int min = Integer.MAX_VALUE;
int[] values = {5, 3, 8, 1, 9};
for (int v : values) {
    if (v < min) min = v;
}
System.out.println(min); // Prints "1".

Notes

MAX_VALUE and MIN_VALUE are often used as initial values in minimum/maximum search algorithms. If a calculation may exceed the range of int, use the long type or the BigInteger class. Overflow does not throw an exception — it silently returns an incorrect value, so be careful.

Conversion methods such as toBinaryString() return an unsigned representation. Converting a negative number produces its 32-bit (or 64-bit) unsigned bit pattern.

For converting strings to numbers, see Integer.parseInt() / Double.parseDouble().

If you find any errors or copyright issues, please .