Static typing is explicitly pointing out types of variables at compile time. Languages that are statically typed make you declare the types of variables before you can use them. Consider an example from C
int num; num = 5; // or you can skip the verbosity and do: int num = 5;
The mention of
int before the variable
num is to specify that the variable will hold an integer. So, the programmer is expected to declare the types of variables. In some other cases, the type is inferred by the compiler. This is seen in a language like Rust where a variable may be declared without a type explicitly noted by the programmer, like so:
let num = 5;
The fact that
num is not explicitly declared with a type does not mean it's not statically typed or that you could type:
num = "Hello, world!";
without Rustc screaming at you. The compiler is only able to infer its type from the value assigned to it to know that it is of type
i32, meaning a signed integer worth 32 bits of memory (Rust types rival Elon Musk when they want to).
number = 5 print(number) number = "Hello, World!" print(number)
as you will, without any consequence.
Well, both typings have advantages and disadvantages.
For static typing, you lose flexibility and takes a while to get used to. At some point types get in the way of productivity, as you may write functions that may accept various types. Statically typed languages would generally give you generics or operator overloading to work around this.
An advantage you get is sanity and easier debugging. It is easier to predict how a program would behave and bugs could be caught easily. Linters, auto complete and formatting features in code editors love statically typed languages too.
Dynamic typing on the other hand is easier on beginners and requires little work from the programmer. A downside is painful debugging and unexpected behaviors. In Python, a function like so:
def add(a, b): return a + b
would work on both numbers and strings. In addition, you'll get no warnings if you try to run this function against a string and an integer until you execute the code.
For now, we have these types. Perhaps, you good reader, can invent one more. In this article, you learned about static and dynamic typing and the differences between them, advantages and disadvantages of each, and most importantly: that you can make yours and your name will be etched in the pages of history.