Learning about data types and structures
In computer science, data types and structures are two distinct things:
- A data type is a basic classification. Some data types include integers, float, and strings.
- Data structures use multiple types of data types. They can organize the information into the memory and determine how we access the information.
Let's look at these in more detail in the following sections.
Data types
As mentioned, data types are basic classifications. They are variables that are used throughout a program and can only exist with one classification. There are different classes of data type. We will focus on primitive and abstract data types for now, but we will revisit this topic as we move through problems and design solutions.
Primitive data types include byte, short, int, long, float, double, Boolean, and char:
- A byte can store numbers from -128 to 127. While these numbers can be stored as integers, or int, a byte uses less storage, so if we know the number is between those values, we can use a byte data type instead.
- A short is a number between -32,768 and 32,767.
- An integer, int, is used to store numbers between -2,147,483,648 and 2,147,483,647.
- Long is used to store numbers from -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
- A float allows us to save a decimal number.
- Decimal numbers can also be saved as double, which has more precision than a float.
- Boolean values are data types that are either
True
orFalse
. So, a variable can be saved such that when its value is printed, the result will be saved as true or false. - Char is used to save a variable as a single character.
We'll look at data structures in the next section.
Data structures
As mentioned under the Coding theory section earlier in this chapter, data structures are used to collect and organize data in the most efficient and effective way possible. Data structures can be primitive, such as the built-in data structures in software, or abstract. Primitive data structures can also be defined using programming languages, but they are pre-defined. Some of the primitive data structures include the data types listed in the previous section, such as chars and Boolean structures.
Abstract data types (ADTs) include the information for the structure and design of data types. Abstract data structures include arrays and two-dimensional arrays, stacks, trees and binary trees, linked lists, queues, and more, as mentioned in the Coding theory section earlier in this chapter. Lists can contain multiple instances of the same data values. These lists are countable, so we can find how many elements are in the list, reorder them, remove items, add items, and so on. Lists are widely used as linked lists, arrays, or dynamic arrays:
- A linked list means that each data element in the list is connected, or points, to the next one, regardless of where they are stored within the memory.
- An array is ordered. The elements are read in order to be able to make sense. Think of an array as reading this sentence. You don't read the sentence as "array an think reading as this of sentence." We read the sentence in order, from left to right, not in a jumbled order.
- Dynamic arrays can be resized, which is important when choosing a data type.
A stack ADT is a collection of elements and has two operations – push and pop. A push is used to add an element to the collection while a pop removes the most recent element.
A queue ADT is a linear data structure. As with a stack, we can add or remove elements. However, in a queue ADT, the point of deletion and the point of insertion are done at two different ends.
As mentioned before, the data structures are concrete implementations of data types. How we add or remove elements from a collection, for example, is the data structure.
This can all be slightly confusing, but we will be learning more about them through examples in later chapters. For now, understanding the definitions and simple examples is enough.