What are the basic data types and why are they important?
In this post, we will look at the basic data types, why they are essential, and where to use them! You will get to know integers, floats, strings, and more!
- Data Types Introduction
- Basic Data Types
- Pracitical Example
- Conclusion of the basic data types
Data Types Introduction
One error that you encounter in type-unsafe languages is that you can pass any type as the value of your function. For example, you have a function that works with numbers, but you pass it a string. But, how should the program know what to do with the value?
In type-safe languages on the other you need to know the basic data types, because you how to use them to create variables. Type-safe languages for example are C# or Java.
Basic Data Types
A commonly used numeric data type is the Integer. It stores whole numbers, for example, 1, 90, or 1999, with a maximum and minimum value determined by the number of bits!
In addition to the basic 32 Bit Integer, there are multiple variations with more or fewer bits. They are commonly referred to as follows:
- byte: 8 bit (-128..127)
- short: 16bit (-32768..32767)
- int: 32 bit (-2147483648 ..2147483647)
- long: 64 bit (-9223372036854775808..9223372036854775807)
The following visualization shows the possible numbers and what happens if you want to add or remove a value from the borders!
Floats are the next numeric datatype on this list, but they contain floating-point numbers in contrast to integers. A floating-point value, for example, is the number: 1,5.
Contrary to integers, the floating-point borders aren’t whole numbers. For floats, they are from -3.4E+38 to 3.4E+38.
Double works exactly as float, just as a 64-bit version. Thus the borders are from -1.7976931348623158E+308 to 1.7976931348623158E+308.
From these borders, we can say that you should use both floating-point data types in the following scenarios:
The character is a super basic data type that stores precisely one letter, a digit, or other symbols. It has the size of 1 byte and thus uses 8 bits.
Strings are sequences of characters and they store text. As the character data type, the string can also store digits, but in the form of text.
A simple example of a string is: “hello world”, but it could also be “hello world 5!”
Booleans represent a single statement, true or false. In most modern programming languages, you can assign and use the value exactly like that. In older languages, for example, in C, booleans were stored as 0 and 1.
The enum data type is a collection of unique values that you can compare and use as these. The values inside of an enum are text-based but also have an integer representation on the other hand.
The following enumeration of music genres visualizes that pretty good:
- 0 = Classic
- 1 = Rock
- 2 = Pop
- 3 = Rap
Thus you can use the numeric or text-based representation of your enum for different actions like comparisons.
The Array is a list of values of a certain data type. In C, for example, strings were stored as arrays of characters. That means that the string “hello” was stored as the array [‘h’,’e’,’l’,’l’,o].
You can retrieve any value of an array by the index. The following visualization shows how this access would work:
In this example, we have a simple form, and you have to assign the proper data types into the given fields. (Press on the fields to reveal the solution!)
Conclusion of the basic data types
In this post, we learned why it is crucial to know your data types, and then we looked at the most commonly used ones like integers, floats, strings, and arrays.
If you want more information about a certain data type you can click on the heading and get directed to a more in-depth article about it!
I hope this short list helped you in getting to know these types. If you are interested in more topics concerning programming, feel free to subscribe to my newsletter and get updates on my most recent post.