Data types in programming ~ By albro

avatar



One of the main components of any application is the data it uses. Familiarity with data types in programming helps us use them in the right place. This will improve the performance and speed of our programming. In this post, we will review the basic data types in programming without depending on the specific language.

Perhaps, in a general view, the main work that a program does can be considered processing. Processing consists of two main parts:

  • The operations that are performed.
  • Data to be processed.

It doesn't matter how our data is entered into the program. We may have taken them from the user, have already saved them, or may have been placed by us as programmers in the program codes.

But why is it really important to know programming data types?

First of all, to store data, we need to identify their type. Secondly, for processing, we have to perform operations on this data. What operators each data type accepts is very important.

For example, in everyday conversation, we know that we can add and subtract numbers together, but these operations will be meaningless for the sentences we speak!

If we can't use data properly in our programming, the chances of us creating a good program will decrease. In the following, we will first get acquainted with the data types and then we will examine the 6 main types together.

Data types in programming

The data type can be examined from 2 points of view. Both views are correct in their place and there is no change in the essence of the story.

The first category is the category based on whether that data type exists by default in the programming language or we have to define it. In some sources, these two categories are also called simple and complex (or standard and definable) data.

Data defined are types that exist in a programming language. A programmer can use that data and the operators between them without any special complexity.

As we become more professional programmers, we may need data types that do not exist by default or whose structure has certain complexities. In such cases, we have to create a structure for the type of data we want.

The second category that is used for data in programming is based on the data type and the operators between them. In most programming languages, there are the following 5 data types:

  • Numerical data
  • Textual data
  • logical data
  • Collection data
  • Structured data



I will examine the first three types together. The structure of these data is different and the operators defined for them are also different.

Collection data is defined as a collection of the first three types. For example, we create a set of 10 numeric data.

Structured data is data that has more complexity than a single type. Usually, each structured data consists of several different data types and has unique characteristics.

Six types of programming data

The first three types that I mentioned in the previous section appear in different ways in different programming languages. Regardless of the programming language, we divide this data into 5 subcategories:

  • Numeric data to integer data and decimal data
  • Text data to character data and text string
  • logical data

In addition to these five items, we will get to know a slightly strange data type at the end of this section.

All types of programming data are stored in computer memory and processor in binary form (or 0 and 1). Therefore, the memory occupied by each type of data varies depending on its size.

Integer in programming

Integer (or int) are the same integers that we met in mathematics. Negative, zero and positive numbers are all included in the category of integers in programming. Numbers like -419, 14 or 25478 are integers.

In some programming languages such as C or Java, integer numbers contain a certain range. For smaller or larger ranges, we have different data types that we can use depending on the need and the amount of occupied space.

But in other languages where we don't usually need to define the data type, such as Python or even PHP, the decision to use a more precise type of storage is usually up to the interpreter of the language. Of course, in some of these languages, it is also possible to define the exact data type of a variable.

Some data types in the set of integers in programming are given in the following table:

TypeRangeMemory
int8(byte)+127 to -1281 Byte
short(int16)+32767 to -327681 Byte
long+2147483647 to -21474836482 Byte

Decimal number type

Floating point or decimal numbers in programming are usually known as float. Float numbers allow us to have numeric data with a decimal part. The method of storing a decimal number is different from an integer in memory and the way of performing operations between them is different. For this reason, we take decimal numbers in programming differently from integers.

In some programming languages, Double data type is used instead of Float to display numbers whose decimal value is very high.

Similar to integers, float decimal numbers are also divided into different subranges in some programming languages. Types such as Real, Single or extended.

Logical data type in programming

Logical data, also called Boolean data, can only hold two values. These values are defined as True and False. These two items are considered equal to 1 (true) and 0 (false).

For this reason, only two true or false values are included in Boolean data, it is also called logical data. We can apply algebraic operations (Boolean algebra such as AND, OR, NOT, etc.) to these values.

In conditional commands, these types of data and expressions are mostly used to check the status. For example, we want to know whether the X setting in the software is on or off.

Character data type

Character data, known as Character or char, contains one character from ASCII characters. The character data type in programming can hold a one-byte character.

Some examples of characters are: a s R # - (space character)

Note that these characters are not the name of a variable but a value. In order not to confuse the character with the variable name, to display each character, we enclose it in quotation marks (' '). As a result the values 'a' and 'P' or '!' They count as one character.

Suppose we have defined a character as '9'. Is it possible to perform mathematical operations on this value?

The answer is no! This data is defined as a character and not a numerical data! As a result, it cannot be treated like numbers, but will have the same behavior as other characters.

Note that the char data type in programming can only store one character. Consequently, the value 'sa' is not a character data; Rather, it is a string type. we will examine below.

Text string data

Almost one of the most used data types in programming is the text data type in the form of a string. A string is a set of characters arranged in order. For example, "Albro" is a text string and consists of 5 English characters.

We use the string data type to store people's names, texts, and anything that consists of a set of letters (characters).

In most languages, various operators and operations are defined for text strings. Working with strings is one of the beautiful challenges of programming.

In most modern languages, the String data type exists independently. But in some languages (for example C), we need to consider a set of characters to define a string. It means that string data does not exist independently and we have to define it ourselves.

Null data type or void in programming

The last type of commonly used data is null data or void! This data is null! 😐 In fact, when a variable or a function has no specific value, that value is called void.

Let me make it easier to understand this data type in programming with an example. Suppose you ask your friend to do something. Regarding the result, two cases can be considered:

  1. We ask our friend to report the result of his work in the form of a data type (number, string or Boolean).
  2. It is important for us to get the job done and we don't want any reports from him/her.

In the second case, we say that our friend's answer is of void type. Because he didn't give any answer and we didn't need it either.

Sometimes this keyword is used to declare that there is nothing in a variable.

This data type is used to represent "none" in programming.

Posted Using LeoFinance Beta

Posted Using LeoFinance Beta



0
0
0.000
1 comments
avatar

Congratulations @albro! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You published more than 10 posts.
Your next target is to reach 20 posts.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out our last posts:

LEO Power Up Day - April 15, 2023
The Hive Gamification Proposal
Support the HiveBuzz project. Vote for our proposal!
0
0
0.000