This is another very important topic in the context of coding. Programming Languages can be classified based on their type safety. Basically you can have two kinds of type safety-
- Strongly Typed
- Weakly Typed
Some Languages fall into the Strongly Typed Category and some of them fall into the Weakly Typed Category. Let’s see what to they mean-
A Programming Language is said to be strongly typed if it forces us to define a datatype for each variable. By now, you might be knowing what datatypes are. Some of the common datatypes are int, char, boolean, float etc. It basically defines the type of the data. Whether the data is a number, character or a string.
This is a whole number. And so we need to define it as an integer. In order, to use this value we have to store it in a container known as a variable.
Let’s name the variable as “apple”.
We store the value in this variable. And since the contents of the variable is a whole number, we define the variable to be an Integer.
So, we have to write something like this-
We have a pink box (known as a variable) named Apple. And in that box, we store a value called 56.
A Strongly typed Language forces us to define the type of a variable. In this case, had we been coding in a strongly typed language, we would have written int apple=56 .
Whereas in a weakly typed language, we are not forced to define the type of the variable. We could have simply written apple=56; and it would have worked!
And In a weakly typed language we can mix two unrelated types. By “Unrelated” what I mean is that an Integer and a Character or an Integer and a Boolean Value. Related Types could be Integers and Decimals.
So, in a weakly typed language, we could add a number and a character and it would allow us to do it.
This is what we would have written in Perl, which is a weakly typed language. In Perl, the variables are prefixed with a dollar sign. So, we have a variable named apple which stores a whole number 45. And we have another variable named ch which stores a string named “and”. In the third line, we try to combine the two unrelated types. And finally, we try to print that result.
And the result is??
It would be 45and
So, this essentially demonstrates that weakly typed languages have no problem mixing unrelated types.
Some of the strongly typed languages are- Java, C# and a weakly type Language would be Perl.