Google News
logo
Dart - Interview Questions
Explain the difference between static typing and dynamic typing in Dart
The difference between static typing and dynamic typing in Dart lies in how variables are handled with respect to type checking at different stages of the development process.

1. Static Typing :

   * Static typing refers to the practice of explicitly declaring the types of variables at compile-time.

   * In Dart, when a variable is declared with a specific type, its type is known and checked by the compiler before the code is executed.

   * Static typing helps catch type-related errors early during the development process, as the compiler can detect potential type mismatches and raise compilation errors or warnings.

   Examples of static typing in Dart :
     String name = 'John'; // A string variable
     int age = 25;         // An integer variable​
2. Dynamic Typing :

   * Dynamic typing, also known as type inference, refers to the ability of a programming language to determine the type of a variable dynamically at runtime, without explicit type declarations.

   * In Dart, variables can be declared without specifying their types, and the type of the variable is determined based on the assigned value.

   * The type of a dynamically typed variable can change during runtime, allowing flexibility in assigning values of different types.

   * Type checking for dynamically typed variables occurs at runtime, and type-related errors might surface during execution if incompatible operations are performed on the variables.

   Examples of dynamic typing in Dart :
     var name = 'John';    // The type of 'name' is inferred as String
     var age = 25;         // The type of 'age' is inferred as int
     var value = 3.14;     // The type of 'value' is inferred as double​
Advertisement