Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The int keyword in Java is a primitive data type that represents a 32-bit signed two’s complement integer. It is the default data type for integral numerical values in the Java Virtual Machine (JVM).

Technical Specifications

  • Memory Footprint: 32 bits (4 bytes)
  • Minimum Value: -231 (-2,147,483,648), programmatically accessible via Integer.MIN_VALUE
  • Maximum Value: 231-1 (2,147,483,647), programmatically accessible via Integer.MAX_VALUE
  • Default Value: 0 (Applies to class/instance fields and array components; local variables must be explicitly initialized before use)
  • Wrapper Class: java.lang.Integer (Used for generics and object-level operations, subject to autoboxing/unboxing)

Syntax and Literal Representations

Java supports multiple literal formats for assigning values to an int. Since Java 7, underscores can be inserted between digits to improve readability without affecting compilation.
// Standard declaration and initialization
int standardValue = 42;

// Literal base representations
int decVal = 26;           // Decimal (Base 10)
int hexVal = 0x1a;         // Hexadecimal (Base 16, prefix 0x or 0X)
int octVal = 032;          // Octal (Base 8, prefix 0)
int binVal = 0b11010;      // Binary (Base 2, prefix 0b or 0B)

// Digit separators (Java 7+)
int maxInt = 2_147_483_647;

Type Conversion and Casting

Because int sits in the middle of Java’s numeric primitive hierarchy, it is subject to specific casting rules. The hierarchy flows as byteshortintlongfloatdouble, with the 16-bit unsigned char type also widening directly to int. Widening Conversion (Implicit) Smaller primitive types (byte, short, and char) are automatically promoted to int without explicit casting.
byte byteVal = 127;
short shortVal = 32000;
char charVal = 'A'; // Unicode value 65

int widenedFromByte = byteVal;
int widenedFromShort = shortVal;
int widenedFromChar = charVal; // Results in 65
Narrowing Conversion (Explicit) Assigning a larger or floating-point type to an int requires an explicit cast. This operation truncates floating-point decimals and drops high-order bits from larger integers, which can result in sign inversion or data loss.
long longVal = 5_000_000_000L;
double doubleVal = 42.99;

int narrowedFromLong = (int) longVal;     // Results in 705032704 (Data loss)
int narrowedFromDouble = (int) doubleVal; // Results in 42 (Truncation)

Numeric Promotion

A critical behavior of int in Java is its role in numeric promotion. When performing arithmetic or bitwise operations on smaller integral types (byte, short, and char), the JVM automatically promotes the operands to int before the operation is performed. The resulting value is always an int.
byte a = 10;
byte b = 20;

// byte c = a + b; // Compilation error: possible lossy conversion from int to byte
int c = a + b;     // Correct: a and b are promoted to int, yielding an int result

char x = 'A';
char y = 'B';
int z = x + y;     // Correct: 65 + 66 = 131

Overflow and Underflow

Because Java’s int has a fixed 32-bit size, arithmetic operations that exceed Integer.MAX_VALUE or fall below Integer.MIN_VALUE do not throw an exception by default. Instead, they silently wrap around due to two’s complement arithmetic.
int max = Integer.MAX_VALUE; // 2,147,483,647
int overflow = max + 1;      // Results in -2,147,483,648 (Integer.MIN_VALUE)

int min = Integer.MIN_VALUE; // -2,147,483,648
int underflow = min - 1;     // Results in 2,147,483,647 (Integer.MAX_VALUE)
To prevent silent overflow, Java 8 introduced the Math.*Exact family of methods (e.g., Math.addExact(), Math.subtractExact(), Math.multiplyExact()). These methods enforce strict bounds checking and throw an ArithmeticException if an operation overflows the 32-bit limit.
// Throws java.lang.ArithmeticException: integer overflow
int safeAdd = Math.addExact(Integer.MAX_VALUE, 1); 

Unsigned Integer Operations

By definition, the int primitive is signed. However, starting in Java 8, the java.lang.Integer API provides static methods to treat the 32 bits of an int as an unsigned integer, shifting the representable range to 0 through 232-1 (4,294,967,295).
// Parsing an unsigned 32-bit value that exceeds Integer.MAX_VALUE
int unsignedInt = Integer.parseUnsignedInt("3000000000");

// Performing unsigned arithmetic
int quotient = Integer.divideUnsigned(unsignedInt, 2);
int remainder = Integer.remainderUnsigned(unsignedInt, 10);
Master Java with Deep Grasping Methodology!Learn More