Strong vs Weak Typing
Strong vs Weak Typing
To start off...
Let's not confuse Strong / Weak Typing with Static / Dynamic Typing.
They're similar in a way but fundamentally different.
Strong / Weak Typing is about how strictly types are distinguished. (For example: whether the language tries to do an implicit conversion from strings to numbers.)
Static / Dynamic Typing is about when type information is acquired (either at compile time or at runtime).
Now that we cleared that up, let's go deeper into Strong vs Weak Typing
Most developers agree that the essence of a strongly typed language is the fact that it won't convert a variable or value's type to suit the current situation automatically. This means that "1234" will always be treated as a string, and won't be usable as a number without manual conversion or intervention.
Let's look at this Python example:
x = y;
y = "2";
z = x=y; #This will fail
This addition operation to get the variable z will fail because the two variables (x and y) which we tried to add together were different types. Python will throw an error that states that adding an int and a string isn't supported.
Examples of strongly-typed languages:
The opposite of strongly typed. The interpreter or compiler will attempt to make the best of what's given by using variables in ways that might seem confusing at first, but make sense once we understand what they're doing. Meaning, in some cases for example, an integer might be treated as if it were a string, in order to suit the context of the situation.
const x- 1;
const y = "2";
const z = x + y; // z will equal 12
This returned 12, instead of 3. This is because rather than performing a math operation as we requested, it treated both values as strings, and concatenated them together, resulting in the string "12"
Examples of weakly-typed
Hopefully these last two posts helped you understand the differences between Statically and Dynamically Typed as well as Strongly and Weakly Typed languages.
Please let us know in the comments if you're still confused and we'd be happy to explain more in depth!
In case you missed it...
Check out our previous post about Static vs Dynamic Typing
Join our tech and design community! @hackuniversity