In programming, data types are a fundamental tool in software development. They define what values can be stored in a variable or data structure.
You know the saying:
You can’t add apples and oranges
Well, in the same way, it’s quite complicated to add a number and text (in fact, you can’t add them 😅).
That’s why data types are important because they help the compiler or interpreter understand and manage information appropriately, and know how to manipulate and handle variables.
It’s important to say that your program will have data types (whether you see them or not). In dynamically typed languages, the interpreter hides much of the data management from you. But the data types are still there.
If there were no data types, the computer literally wouldn’t know how to work with things. It wouldn’t know how to store them, how to manipulate them, how to add them. Without data types, a computer cannot function
A typical programming language supports several data types. But, in general, we could classify them into:
Primitive Data Types
Primitive data types are those that are built into the programming language and do not require prior definition.
These data types are found in most programming languages and can vary from one to another, but generally include:
Compound Data Types
Compound data types are those that contain more than one value. These types can be defined by the programmer or can be built into the programming language.
Some examples of compound data types are:
Collections are compound data types that allow storing multiple elements of the same type in a single structure.
Some common examples of collections are Arrays, Lists, or Dictionaries.
Groupings are compound data types that allow combining different elements into a single entity.
Some common examples of groupings are Structs, Objects and Tuples.
Enumerations are compound data types that represent a finite set of possible values. These values are usually constants and are defined in advance.
Enumerations are useful when you want to restrict a variable or parameter to a specific set of options. For example, a DayOfTheWeek enumeration could have values like Monday, Tuesday, Wednesday, etc.
Unions (also known as variant data types or variants), allow representing a type compatible with different different types. A variable of a union can store one of the several types specified in the union.
For example, a Result union could contain a numeric value or an error message, depending on the context.
Reference Types
Reference data types are used for indirect manipulation of data stored in other variables. These data types do not store the value directly, but rather refer to the location in memory where the value or function is located.
Some examples of reference data types are:
A Reference to another variable is a data type that allows accessing and manipulating the value stored in an original variable through a reference.
Instead of copying the value of the variable, a Reference is created that points to the memory location where the value is located.
This means that any change made through the Reference will also affect the original variable. Variable references are especially useful when working with large data structures or when you want to share the same data between different parts of a program.
A function reference is a data type that allows storing the memory address of a function. This allows the creation of advanced structures within a program.
Some examples of function references are Callbacks, Events, Filters and Selectors.
Abstract Data Types
Abstract Data Types (ADTs) are data types that define a public interface and hide their internal implementation. This means that users of an ADT only need to know how they can interact with it (without needing to know the details of how it is implemented).
Examples of abstract data types are Abstract Classes and Interfaces.
