Skip to main content
TOPIC 1 OF 9 · 2 MIN READ

Static vs Dynamic Typing

Typing systems in programming define how variables are declared and how their types are handled during code execution. Static typing and dynamic typing represent two different approaches to type-checking in programming languages. Knowing their differences can help in choosing the right language for a specific project.

Key concepts

Statically Typed Languages

Variables have a fixed type determined at compile-time. Examples: Java, C++, Swift.

Example
int number = 10; // type is explicitly declared

Dynamically Typed Languages

Variables are not bound to a specific type and are determined at runtime. Examples: Python, JavaScript.

Example
number = 10 # type is inferred dynamically

Step by step

Static Typing:

  • Errors are caught at compile-time.
  • Improves performance and reduces runtime bugs.
  • Requires more boilerplate code (explicit type declarations).

Dynamic Typing:

  • Errors are detected at runtime.
  • Increases flexibility and speeds up prototyping.
  • Risk of unexpected type-related errors during execution.

Examples

Statically Typed:

double price = 9.99; // Explicit type assignment
price = "ten"; // Compile-time error

Dynamically Typed:

let price = 9.99;
price = "ten"; // No error at declaration, may fail during runtime

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Identify whether the following snippets use static or dynamic typing.
  • Write examples of declaring variables in a statically typed language and a dynamically typed language.

Summary

Static typing provides safety and efficiency but requires more setup. Dynamic typing offers flexibility at the cost of potential runtime errors.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus