> ## 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.

# Java int

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:** -2<sup>31</sup> (-2,147,483,648), programmatically accessible via `Integer.MIN_VALUE`
* **Maximum Value:** 2<sup>31</sup>-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.

```java theme={"dark"}
// 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 `byte` → `short` → `int` → `long` → `float` → `double`, 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.

```java theme={"dark"}
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.

```java theme={"dark"}
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`.

```java theme={"dark"}
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.

```java theme={"dark"}
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.

```java theme={"dark"}
// 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 2<sup>32</sup>-1 (4,294,967,295).

```java theme={"dark"}
// 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);
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
