Skip to main content
The num type in Dart is an abstract class that serves as the root of the numeric type hierarchy. It provides a common interface for all numeric values and is the direct superclass for Dart’s two concrete numeric types: int (integer values) and double (64-bit double-precision floating-point values). Because it is abstract, num cannot be directly instantiated; it acts as a polymorphic container for its subclasses.

Type Resolution and Polymorphism

When a variable is declared as num, the Dart compiler permits it to hold either an int or a double. The exact type is resolved at runtime based on the assigned value.

Platform-Specific Memory Representation

The underlying memory representation of num subclasses depends on the compilation target:
  • Native (Dart VM / AOT): int is represented as a 64-bit two’s complement integer. double is represented as an IEEE 754 64-bit double-precision floating-point number.
  • Web (JavaScript): Because JavaScript lacks a distinct integer type, both int and double are mapped to JavaScript’s Number type (IEEE 754 double-precision float). Consequently, on the web, int precision is restricted to 25312^{53} - 1 (the maximum safe integer in JavaScript).

Core Properties

The num class defines several standard properties to evaluate the state of the numeric value:

Core Methods

num provides a standard suite of mathematical and transformation methods inherited by both int and double: Type Conversion:
Rounding and Bounding:

Parsing

The num class provides static methods for parsing string literals into numeric types. The parser automatically infers whether to return an int or a double based on the presence of a decimal point or exponent.

Operator Restrictions

While num supports standard arithmetic operators (+, -, *, /, %, ~/), it does not support bitwise operators (&, |, ^, ~, <<, >>, >>>). Bitwise operations are strictly defined on the int class. To perform bitwise operations on a num variable, it must first be explicitly cast or converted to an int.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More