When we talk about programming languages, we often come across the typing of a language - how it is "typed".

We can say there are two categories of defining the typing of a language:

  1. Static vs. Dynamic
  2. Strong vs. Weak

Static vs. Dynamic

This categorization of typing represents WHEN the types are checked in the process of running your program.

When you run your program, the codes you have written are first compiled into another low-level language that the computer can directly interpret. This period of your code being compiled is referred to as "compile time".

After compilation, the computer then executes your code - or run the program. This period of your codes being run is referred to as "runtime".

Statically typed:

meaning: types are checked during COMPILE TIME
examples: C, C++, C#, Java, TypeScript (optionally static)

Dynamically typed:

meaning: types are checked during RUNTIME
examples: JavaScript, Python, Ruby, PHP


Strong vs. Weak

This categorization of typing represents HOW STRICTLY the type restrictions are enforced.

As a very common example, JavaScript automatically converts the types according to its own set of rules when you perform certain operations using data of different types. JavaScript is a weakly (or loosely) typed language.

For example,

10 + '5'
// the output is '105'
// number + string => string + string
// JS auto-converts the number to string and concatenates the two strings

On the other hand, when you try the same operation in Python, you get a TypeError. In Python, you won't get any automatic type conversions. You have to explicitly convert the data type. Python is a strongly typed language.

Strongly typed

meaning: the rules/restrictions relating to data types are STRICTLY enforced
examples: Python, Ruby, Java, C, C++, C#, TypeScript

Weakly typed

meaning: the rules/restrictions relating to data types are LOOSELY enforced
examples: JavaScript, PHP

0개의 댓글