DEV Community

Funwie
Funwie

Posted on

Data Types and Static vs Dynamic Typing

Quick and short explanation of Data Types and Static vs Dynamic Typing

Data Types

Types give data meaning. There exist many data types such as numbers, strings, files, arrays, objects, and user-defined for us to use in our programs. Types differentiate data for processing. The type of data determines the operations that can be performed with the data. How will you add binary data without knowing if they are number types?

All data that you use in your programs have a type.

Memory

Data is stored in locations in memory, read, and processed.

Variables

Variables are identifiers that hold the memory location of data. We write code using variables. Our programs take memory locations from variables, read data stored at those locations, and use the data to do it's calculations and to take decisions.

Variables have types. How a variable's type is determined and which type of data can be assigned to a variable depends on the typing rules of the language.

Static Typed Language

  • Variables are identifiers that have types.
  • A variable allows assignment of only data that has same type as the variable.

Using Static Types

integer accountNumber = 1000;

accountNumber is a variable that is of type integer
We have signed a contract with the compiler that accountNumber should be assigned only integer type data.

accountNumber = Image // error

The compiler disagrees. We have broken the contract which clearly states that accountNumber should only be assigned integer data types.

Static typing checks are done by compilers at compile-time. Compilers know all the variables you've declared and their types (the contract), so any attempt to break the contract by assigning a different data type to a variable or performing incompatible operations will be flagged as illegal.

integer number = 20 // correct. 20 is integer type
number = "Twenty" // error. "Twenty" is not integer type
double coins = 99.9
string name = "money"

coins / name

The division operation above errors at compile-time. The compiler knows the types of both variables, therefore will flag operations that are illegal. You can not divide a number by text.

In static typed languages, the type of a variable never changes after declaration, and a different data type can not be assigned to the variable. Your program will not compile if there exist illegal type assignments or operations.

Example static typed languages are Java, C#, C++

Dynamic Typed Language

  • Variables are identifiers that do not have types.
  • Variables allow any data type assignment.
  • Variables get their type from the type of data assigned to the variable.
dynamicVariable = 100

The compiler knows that 100 is a number, therefore, dynamicVariable will be type number as long as it holds a number data type.

dynamicVariable = Image

Now, the data is of type Image, therefore, dynamicVariable's type is Image.

When the program is running (runtime), the type of dynamicVariable is determined by the type of data that is assigned to it.

// true type is Boolean, therefore dynamicVariable is boolean
dynamicVariable = true 

// Person type is Object, therefore dynamicVariable is Object
dynamicVariable = Person()
dynamicNumber = 100
dynamicString = "One hundred"

dynamicNumber / dynamicString

The division operation above will error at runtime because it is only during execution that the program will check the type of variables and realise that the operation can not be performed with operands of type string and number.

In dynamic typed languages, the variable's type changes dynamically to take the type of data assigned to the variable. Your program will compile if there exist illegal operations, but it will crash when it encounters the illegal operation at runtime.

Example dynamic typed languages are Python, JavaScript, PHP

Casting

To change the type of the data to match the type of the variable.
Casting is done implicitly by the compiler or explicitly by the programmer.

integer number = 10
double height = (double)number

Take the data of number that is integer type, convert the data into a double type and assign the converted double data to height double type variable.

This is a very short explanation with so much information lacking (omitted to keep the post short). There are countless resources out there to read and most importantly to experiment.

Find an online IDE for any of the example languages and try out the concepts for yourself.

Top comments (0)