Skip to main content
An int is a primitive numeric data type representing a non-fractional value, implemented as a subtype of num. It serves as the standard representation for integers within the Dart type system.

Platform Implementation

The internal representation of int is platform-dependent:
  • Dart VM (Native): Represents a strictly 64-bit two’s complement signed integer.
    • Range: -2^63 to 2^63 - 1.
    • Overflow: Arithmetic operations that exceed this range wrap around.
  • Dart Web (JavaScript): Maps to a JavaScript Number (IEEE 754 double-precision floating-point).
    • Range: The safe integer precision range is -(2^53 - 1) to 2^53 - 1. Values exceeding this range may suffer precision loss due to the limitations of the floating-point mantissa.
    • Bitwise Operations: Operations are truncated to 32 bits to align with JavaScript bitwise behavior.

Syntax and Literals

Dart supports integer literals in decimal and hexadecimal formats.
// Decimal literal
int decimalValue = 42;

// Hexadecimal literal (prefix with 0x)
int hexValue = 0x2A;

// Underscores for readability (ignored by compiler)
int largeValue = 1_000_000;
Note on Scientific Notation: Literals using scientific notation (e.g., 1e3) are strictly typed as double, even if the resulting value is a whole number. Assigning a scientific notation literal to an int results in a compile-time error.
// Compile-time error: A value of type 'double' can't be assigned to a variable of type 'int'.
// int scientific = 1e3; 

// Valid: Explicit conversion required
int scientificConverted = 1e3.toInt(); 

Type Safety and Conversion

Dart is strongly typed and does not support implicit widening (coercion) from an int variable to a double. While integer literals can be assigned to double variables (inferred as doubles), typed int variables require explicit casting methods.
  • No Implicit Widening: Assigning an int typed variable to a double results in a compile-time error.
  • Division: The standard division operator / always returns a double, even if the operands are integers and the result is a whole number.
  • Integer Division: The truncating division operator ~/ returns an int.
int x = 10;

// Compile-time error: A value of type 'int' can't be assigned to a variable of type 'double'.
// double y = x;

// Valid: Explicit conversion
double y = x.toDouble();

// Valid: Integer literal assigned to double
double z = 10; 

// Arithmetic resulting in double
double standardDivision = 5 / 2; // Result: 2.5

// Arithmetic resulting in int
int truncatedDivision = 5 ~/ 2;  // Result: 2

Bitwise Operations

The int class supports standard bitwise manipulation. On the web, these operations implicitly truncate the operand to 32 bits.
int a = 3;  // Binary: 0011
int b = 5;  // Binary: 0101

int bitwiseAnd = a & b;  // 0001
int bitwiseOr  = a | b;  // 0111
int bitwiseXor = a ^ b;  // 0110
int bitwiseNot = ~a;     // Inverts bits (Bitwise complement / One's complement)
int shiftLeft  = a << 1; // 0110 (6)
int shiftRight = a >> 1; // 0001 (1)
int unsignedShift = -a >>> 1; // Zero-fill right shift

Key Properties

  • bitLength: Returns the minimum number of bits required to store the integer.
  • isEven / isOdd: Boolean properties indicating parity.
  • sign: Returns -1, 0, or 1 depending on the sign of the integer.

Comparison with BigInt

For integers exceeding the 64-bit range (native) or the 53-bit safe precision range (web), Dart provides the BigInt class. While int is optimized for performance and maps to CPU registers or JS primitives, BigInt allows for arbitrary precision at the cost of memory and processing speed.
Master Dart with Deep Grasping Methodology!Learn More