liveBook · Manning

Unchecked-inc-int

As explained in Clojure: The Essential Reference

unchecked-inc-int is a Clojure function that increments using Java Integer arithmetic without implicit promotion to Long or overflow checking.

unchecked-inc-int increments a numeric value using Java's primitive int arithmetic. Unlike ordinary incrementing, it does not implicitly promote the value to Long or check for overflow, and it returns an int. section 5.14 section 5.6

Use it when integer-width behavior matters:

  • Pass one numeric argument within the Integer range. section 5.14
  • Receive a java.lang.Integer-compatible primitive int result. section 5.14
  • Expect signed 32-bit wraparound at Integer/MAX_VALUE rather than promotion or an overflow exception. section 5.14 section 5.6

Incrementing inside integer arithmetic

A line-drawing loop uses (unchecked-inc-int x) and (unchecked-inc-int y) while processing coordinates in an integer-based Java array. The same implementation combines it with unchecked subtraction, division, and addition to keep the arithmetic in the int domain.

How Clojure: The Essential Reference covers it

How deep it goes
Explains the argument contract, primitive result, 32-bit wraparound, and profiling considerations, but does not detail JVM implementation internals.
Examples
Uses integer coordinate loops for drawing a line into a multidimensional Java array.
What it assumes
Assumes basic Clojure numeric operations and Java numeric types while establishing the int-versus-Long distinction.
What you can do after
Apply primitive int incrementing in integer algorithms while recognizing wraparound and validating any claimed performance improvement with profiling.
How it connects
Connects primitive int arithmetic with unchecked operators, integer overflow, hashing algorithms, and Java arrays.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.