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 asnum, 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 ofnum subclasses depends on the compilation target:
- Native (Dart VM / AOT):
intis represented as a 64-bit two’s complement integer.doubleis represented as an IEEE 754 64-bit double-precision floating-point number. - Web (JavaScript): Because JavaScript lacks a distinct integer type, both
intanddoubleare mapped to JavaScript’sNumbertype (IEEE 754 double-precision float). Consequently, on the web,intprecision is restricted to (the maximum safe integer in JavaScript).
Core Properties
Thenum 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:
Parsing
Thenum 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
Whilenum 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





