Language: EN

programacion-tipos-de-datos

What are data types

In programming, data types are a fundamental tool for software development. They define the type of information that can be stored in a variable or data structure.

You know it is often said that:

You can’t add apples and oranges

In the same way, it is quite complicated to add a number and text. In fact, they cannot be added 😅.

That’s why data types are important because they help the compiler or interpreter understand and manage information properly, and know how to manipulate and treat variables.

It is 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. But data types are still there.

If there were no data types, the computer literally would not know how to add two things. It wouldn’t know how to store them, it wouldn’t know how to manipulate them. Without data types, a computer cannot function.

A typical programming language supports various data types. However, in general, they could be classified as:

  • Primitive data types
  • Composite data types
  • Reference data types
  • Abstract data types

Primitive Data Types

Primitive data types are those that are integrated into the programming language and do not require a previous definition.

These data types are found in most programming languages and can vary from one language to another, but generally include:

Composite Data Types

Composite data types are those that contain more than one value. These types can be defined by the programmer or integrated into the programming language.

Some examples of composite data types are:

Collections

Collections are composite 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.

An array is an ordered collection of elements of the same type. It can contain a fixed number of elements and each element is accessed by a numeric index. For example, an array of integers could contain the numbers [1, 2, 3, 4, 5].

A list, on the other hand, is an ordered collection of elements that can grow or shrink dynamically. The elements of a list can be of different types and are accessed by their position. For example, a list of words could contain [“hello”, “world”, “programming”].

Aggregations

Aggregations are composite data types that allow combining different elements into a single entity. Some common examples of aggregations are structures, objects, and tuples.

  • A structure is a grouping of variables of different types. Each variable within a structure is called a member. For example, a “Person” structure could have members like “name,” “age,” and “address.”
  • An object is a grouping of data that includes other variables (attributes) and their behavior (methods). For example, a “Car” object could have attributes like “brand,” “model,” and “color,” and methods “accelerate” and “brake.”
  • A tuple is an ordered grouping of elements of different types. Unlike arrays and lists, tuples are usually immutable. For example, a tuple could contain information about a point in coordinates (x, y).

Enumerations

Enumerations are composite 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 “DayOfWeek” enumeration could have values like “Monday,” “Tuesday,” “Wednesday,” etc.

Unions

Unions, also known as variant data types, allow representing a value that can have 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 data manipulation stored in other variables. These data types do not directly store the value, but refer to the memory location where the value or function is located.

Some examples of reference data types are:

Reference to other variables

A reference to another variable is a data type that allows you to access and manipulate 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 changes made through the reference will also affect the original variable. References to variables are especially useful when working with large data structures or when you want to share the same data between different parts of a program.

Reference to functions

A reference to a function is a data type that allows storing the memory address of a function. This allows for advanced structures within a program.

Some examples of function references are events, callbacks, filters, or selectors.

Abstract Data Types

Abstract data types (ADT) 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.