Magic lies here - Statically vs Dynamically Typed Languages
If a language specification requires its typing rules strongly (i.e., more or less allowing only those automatic type conversions that do not lose information), one can refer to the process as Strongly typed, if not, as Weakly typed.
Type checking is all about ensuring that the program is type-safe, minimizing the possibility of type errors. For eg — type error could be a situation where an operation is performed on a data of type integer with the intent that it is a float, or even something such as adding a string and an integer together
a = “5” + 2;
Despite of the fact that in many languages both strings and integers can make use of the + operator, this would often result in a type error because this expression is usually not meant to handle multiple data types.
When a program is considered not to be type-safe, there is no single standard course of action that happens upon encountering a type error. Many Programming languages throw type errors which halts the run-time or compilation of the program, depending on the language type — static or dynamically typed.
Now since we have a basic understanding of what types are and how type checking works, we can start with the two primary methods of type checking: statically and dynamically typed languages.
Static typed languages
A language is statically-typed if the type of a variable is known at compile-time instead of at run-time. Common examples of statically-typed languages include Java, C, C++, FORTRAN, Pascal and Scala.
In Statically typed languages, once a variable has been declared with a type, it cannot ever be assigned to some other variable of different type and doing so will raise a type error at compile-time(some IDE’s generally shows a Red Cross mark denoting the error).
Statically-typed languages require you to declare the data types of your variables before you use them
data = 50;
data = “Hello World!”; // causes an compilation error
Note: the statement above (which binds an integer value, and then binds a string value to the same variable name data) is illegal. But in a dynamically-typed language this sequence of statements is perfectly fine.
- A large class of errors are caught in the early stage of development process.
- Static typing usually results in compiled code that executes more quickly because when the compiler knows the exact data types that are in use, it can produce optimized machine code (i.e. faster and/or using less memory).
For a list of languages with static type checking, see the category for statically typed languages.
Dynamically typed languages
In Dynamically typed languages, variables are bound to objects at run-time by means of assignment statements, and it is possible to bind the same variables to objects of different types during the execution of the program.
Dynamic type checking typically results in less optimized code than static type checking. It also includes the possibility of run time type errors and forces run time checks to occur for every execution of the program (instead of just at compile-time).
Dynamically-typed languages do not require you to declare the data types of your variables before you use them
data = 10;
data = “Hello World!”; // no error caused
The above statement assigns a new value to the same variable data of different data type than the one assigned earlier. The program will execute successfully without any error. This is characteristic to dynamic-typed programming languages.
- Implementations of dynamically type-checked languages generally associate each run time object with a type tag (i.e., a reference to a type) containing its type information. This run-time type information (RTTI) can also be used to implement dynamic dispatch, late binding, down-casting, reflection, and similar features.
- The absence of a separate compilation step means that you don’t have to wait for the compiler to finish before you can test your code changes. This makes the debug cycle much shorter and less cumbersome.
For a list of such languages, see the category for dynamically typed programming languages.
Strongly typed languages
A strongly-typed language is one in which variables are bound to specific data types, and will result in type errors if types do not match up as expected in the expression — regardless of when type checking occurs.
Python is strong-typed, and so is Java.
temp = “Hello World!”
temp = temp + 10; // program terminates with below stated error
Traceback (most recent call last):
File “…\Test.py”, line 48, in NFG
temp = temp + 10;
TypeError: cannot concatenate ‘str’ and ‘int’ objects
In the above example, temp is of str type. In the second line, we tried attempting to add 10 (an int) to a variable of str type. As we can see, a TypeError is returned, indicating that a str object cannot be concatenated with an int object. This is what characterizes strong typed languages: variables are bound to a particular data type.
Weakly typed languages
A weakly-typed language on the other hand is a language in which variables are not bound to a specific data type; they still have a type, but type safety constraints are lower compared to strongly-typed languages.
PHP is weakly-typed, and so is C.
$temp = “Hello World!”;
$temp = $temp + 10; // no error caused
In this example, variable temp is initially a string type. In the second line, we add this string variable to 10, an integer. This is permitted in PHP, and is characteristic of all weakly-typed languages.
In this post, I answered few questions regarding Statically and Dynamically typed languages. I also covered Strongly-typed and Weakly-typed languages.
Just as the assumption that all Strongly-typed languages are Statically-typed, not all Weakly-typed languages are Dynamically-typed; PHP is a dynamically-typed language, but C — also a weakly-typed language — is indeed statically-typed.