Custom Search

Static and Dynamic Typing

This has nothing to do with your keyboard skills....

The 'typing' we are talking about here refers to programming data types and the manipulation of data that is allowed in a specific programming language. It describes the structure of the data and how the types of data are stored in the memory.

Strings, integers, Boolean, Integer, Character and Float are all 'types'.

In terms of programming languages, we can categorize typing into two camps: static typing and dynamic typing.

Static Typing

In a static typed programming language the programmer must declare the variables clearly before using them. So static programming languages don't allow the changing of variable types.

The most popular static typed programming languages are C, C++, and Java. In these programming languages, using a variable if you have not declared it first will result in a compiling error.

As a programmer, you don’t have to define the variables before they are put into use in static typed programs but you do have to declare them.

Dynamic Typing

Dynamic typed programming languages are very different to static typed programming language. A language is dynamically typed if type is associated with run-time values, and not named variables/fields/etc.

Examples of languages that use it are: Perl, Ruby, Python, PHP, JavaScript and Erlang.

In dynamic typed, all the variables must be defined before they are used (for example, if you are planning to use the variable x, you must assign it a value before you use it) but you don't have to 'declare' these variables and they don't have to be 'typed into a particular type'. Therefore dynamic typing is more flexible than static programming because it allows variables to change types and it therefore allows you to write code a little quicker as you do not have to specify types every time.

Dynamic typing is a system in which 'type checking' is performed at run time and not at compilation.

Compiled vs Interpreted

A key difference between static and dynamic programming languages is that static typed programming languages are compiled when executed. Before the run-time each line of code is 'translated' by the compiler and iIf there are errors the program will not run, but the whole process will come to a stop at the compilation stage. Whereas, dynamically typed programming languages are 'interpreted' when executed - no compliation stage is performed beforehand - it runs - errors and all.

Type Checking

In static typed programming languages, types are checked before the run-time. If the code is found to be having an unsuitable type, an error will be thrown up before the run-time. Because static typed programming languages catch errors during the early stages of programming they leave no room for type errors as you continue coding.

Static programming prevents the changing of variables in the program and this feature makes them ideal for long programs.

In dynamic typed programming languages, types are checked during the execution of the code. Even if the line of code has a mistake, it will not be identified before execution of the program. Catching errors during the execution may well slow down the programming process when working on a long complex program.