What are the basic data types and why are they important?

No Comments

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

Data Types are one of the fundamental concepts of programming that you need to know! Some programming languages do the job of using the correct data type for you, i.e., Python or JavaScript. In these languages, you need to understand the existence of the different data types, write better-optimized code, and reduce some errors that can occur in these types of languages.

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

Integer (int)

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:

The following visualization shows the possible numbers and what happens if you want to add or remove a value from the borders!

basic data type: integer overflow

Float (float)

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 (double)

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:

basic data types: floating point type decision help

Character (char)

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.

String (string)

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!”

Boolean (bool)

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.

Enumerated Type (enum)

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:

Thus you can use the numeric or text-based representation of your enum for different actions like comparisons.

Array

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:

Practical Example

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!)

basic data types: example form

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.

Discussion (0)

Add Comment

Your email address will not be published.