Static and Dynamic Typing
Level 1 · How languages differ
When a language checks the type of each value, before the code runs or while it runs. What strong typing adds, and why the two are not the same.
Course contentsLesson 3 of 12
Level 1 · How languages differ
Level 2 · Languages of the web
Level 3 · General-purpose and systems languages
Level 4 · Languages for phones
Level 5 · Data and functional languages
Level 6 · Scripting languages
After this lesson you can
- Tell static typing from dynamic typing
- Explain what a strongly typed language refuses
- Place a language on both scales
12 mincovers 3 glossary entries
Worth reading first: Compilers and Interpreters
When a language checks the type of each value
Every language checks that values are used the right way, but not at the same moment. A statically typed language checks types before the program runs. A dynamically typed one checks them while it runs, at the line that uses the value. Java, C# and Go check first. Python, JavaScript and Ruby check as they go.
In a static language, the type is part of the code. You write int count = 0 in Java, and count can only ever hold a whole number. Try to put text in it, and the compiler refuses to build the program. The mistake is caught on your screen, before any user sees it.
In a dynamic language, a name can hold any type, and the type travels with the value. In Python, count = 0 and then count = "zero" is fine. The check happens only when a line tries to use the value, such as adding it to a number. A mistake shows up as an error at that line, perhaps weeks later.
Static types cost some typing up front and catch a whole class of bugs early. Editors can also use them to suggest names and spot mistakes as you type. Dynamic types make short programs quicker to write. That is why scripts are often dynamic, and big systems built by large teams are often static.
A separate question is how strict a language is. A strongly typed language will not quietly mix types. Python refuses "1" + 1 with a TypeError. A weakly typed language converts one value to make it work. JavaScript turns the 1 into text and gives "11", with no error at all.
So static or dynamic says when types are checked. Strong or weak says how strict the check is. Python is dynamic and strong. JavaScript is dynamic and weak. Java is static and strong. C is static, but it lets a letter be used as a number, so it counts as weak.
People often mix the two scales up and call Python weakly typed because it has no type names in the code. It is strongly typed. It just checks while it runs.
count = 0
count = "zero" # fine: Python is dynamically typed
print("1" + 1) # TypeError: Python is strongly typed
# The same line in JavaScript gives "11", with no error.Worked example
Lucy's bug waits three weeks
Lucy's Python app stores an order count as text by mistake. Nothing breaks until, 3 weeks later, a monthly report adds the counts and crashes.
Her team's Java app has a similar field. There, the same mistake stopped the build on the day it was typed, and it was fixed in 5 minutes.
The words for this lesson3
Check yourself
Python has no type names in its code. Is it weakly typed?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.