Skip to main content

Variable Types (Static vs Dynamic)

NOT AS IMPORTANT AND YOU CAN SKIP IF YOU DON'T UNDERSTAND

When talking about categorizing Data Types (Primitive vs Reference Types) it can be confused with categorizing how variable types are handled (Static vs Dynamic Types).

  • Primitive vs. Reference Types: This categorization refers to how data is stored and accessed in memory. Primitive types represent single values and are stored directly in memory, while reference types store references to objects in memory.

  • Static vs. Dynamic Types: This categorization refers to how variables are declared and typed in a programming language. Static typing means variable types are explicitly declared and known at compile time, while dynamic typing means variable types are determined at runtime based on assigned values.

While these categorizations are related to how data and variables are handled in a programming language, they address different aspects of programming language design and usage.

Static Typing

Static typing is a type system in which variable types are known at compile time and are explicitly declared by the programmer.

Look at notes Software Dev. Phases if unsure about Compile Time (Languages > Software Dev. Phases)

Type Declaration

In statically typed languages, variables are explicitly declared with their types, and type checking is performed by the compiler during compilation.

// Static typing in Java
int num = 10; // num is explicitly declared as an integer
String str = "Hello"; // str is explicitly declared as a string

Type Safety

Static typing helps catch type-related errors at compile time, ensuring that variables are used in a manner consistent with their declared types.

Examples: Languages like Java and C++ are statically typed.

Dynamic Typing

Dynamic typing is a type system in which variable types are determined at runtime based on the values assigned to them.

Type Inference

In dynamically typed languages, types are inferred from the values assigned to variables, and type checking is performed at runtime.

// Dynamic typing in JavaScript
let num = 10; // num is inferred as a number
let str = 'Hello'; // str is inferred as a string

Flexibility

Dynamic typing offers flexibility, as variables can hold values of different types during the execution of the program.

Late Error Detection

Type-related errors may only be detected at runtime, potentially leading to unexpected behavior or runtime exceptions.

Examples: Languages like JavaScript, Python, and Ruby are dynamically typed.

Static v.s Dynamic Typing

Declaration:

Static typing requires explicit type declarations, while dynamic typing infers types based on values.

Checking Time:

Static typing performs type checking at compile time, while dynamic typing performs type checking at runtime.

Error Detection:

Static typing detects type errors early at compile time, while dynamic typing may detect errors at runtime.

Flexibility:

Dynamic typing offers more flexibility, allowing variables to change types dynamically, while static typing provides stricter type enforcement.

Compile Time vs Run Time

Compile time is related to the development and build process, while run time is related to the actual execution of the program. Both are essential stages in the software development lifecycle, but they occur at different points in time.

In Web development terms:

  • If error during Compile time = unable to build
  • If error during Runtime = error during execution of the webpage