Authors Top

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

In this tutorial, we’ll explore statically and dynamically typed languages. We’ll go over what they are, and what they’re used for. We’ll also discuss the differences and similarities that exist between them.

These two terms refer to some basic properties of programming languages that define how they handle variables of different data types.

2. Definitions

Before we go further, let’s define some keywords. A data type in a programming language refers to a characteristic that defines the nature of the value that a data element has. Some common examples include string, integer, character, and boolean.

Every programming language has a system of checking that values have been assigned their correct types, which is known as type checking. Type checking is essential in programming to minimize errors during the execution of programs. This usually occurs at either runtime or compile time. There are two categories of type checking implemented in most programming languages, static and dynamic:

static-vs-dynamic-typing

3. Statically Typed Languages

In statically typed programming languages, type checking occurs at compile time. At compile time, source code in a specific programming language is converted to a machine-readable format. This means that before source code is compiled, the type associated with each and every single variable must be known.

Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.

3.1. Characteristics

For most of these languages, the programmer is required to explicitly state the data type of each variable when the variable is being declared. In contrast, some of these languages don’t have this requirement, and instead implement type inference, where the specific data type associated with a specific variable is determined by the language system.

Another notable characteristic of this category of programming languages is that the detection of errors in variable-data type associations will halt the compilation process until the error has been rectified. For example, consider the following variable assignment. Suppose we want to declare two variables, varA and varB, where the former is an integer and the latter is a string:

Rendered by QuickLaTeX.com

In the assignment statements above, the first line will encounter an error because we’re trying to assign the string hello to an integer variable. The second variable assignment will pass because the string cat is being assigned to a string variable.

Additionally, a variable that’s declared with a specific data type cannot be assigned to another later. For instance, in our example above, varB is a string variable that cannot be later altered to an integer value.

Furthermore, since type checking takes place during compile time, it’s much quicker to detect type errors in programs and address them early. As a result, such programs typically perform better at execution time because type checking has already been completed.

4. Dynamically Typed Languages

Conversely, in dynamically typed languages, type checking takes place at runtime or execution time. This means that variables are checked against types only when the program is executing. Some examples of programming languages that belong to this category are Python, JavaScript, Lisp, PHP, Ruby, Perl, Lua, and Tcl.

4.1. Characteristics

For this category of programming languages, there’s no requirement to explicitly state the data type during variable declarations. The language system is able to detect the type of a variable at runtime. For example, a variable declaration and assignment in this kind of language will be as follows:

Rendered by QuickLaTeX.com

Now, in our example, varC is assigned to 52. Compared to the previous example, we don’t have to specify the data type associated with the variable. In addition, altering the data type of previously declared variables is allowed.

For instance, if we wanted to change the value associated with varC from an integer to a string, we wouldn’t run into any errors. Furthermore, since variable types can be dynamic, the programmer has more flexibility when writing programs. In spite of this, there’s a tendency for slower execution because the type information for every variable has to be retrieved at runtime.

5. Differences

The major differences are:

Rendered by QuickLaTeX.com

Deciding which language to use is usually dependent on the programmer and the purpose of the program. It’s difficult to conclude that one is better than the other, as they both have their own perks and drawbacks.

For instance, if a programmer wants to write and execute code easier, then dynamically typed languages are a good match. However, it’s the responsibility of the programmer to practice good type management. If more rigid code is preferred, then a better option would be a statically typed language.

6. Conclusion

In this article, we reviewed data types, type checking, and the two main techniques used for type checking in programming. Dynamically typed languages offer more flexibility, but with less optimised code, while statically typed languages offer more optimised code with less flexibility.

Authors Bottom

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!