This post is also available in: हिन्दी (Hindi) العربية (Arabic)
While writing a computer program we use variables and constants to accept, process, and output data and information to the user. These data range from numbers to text to boolean values depending on the requirement of a program. The data type is an attribute associated with a piece of data that tells a computer system how to interpret its value.
Let’s understand what is a data type and its importance and the different types of data types used in programming languages.
What is a Data Type?
A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error.
A data type is an attribute associated with a piece of data that tells a computer system how to interpret its value.
A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers.
|Data Type||Used For||Example|
|String||Alphanumeric characters||Hello World|
|Floating Point||Numbers with a decimal point||654.786|
|Character||Text||A, x , i|
|Boolean||Logical values||TRUE, FALSE|
Understanding data types ensures that data is collected in the preferred format and that the value of each property is as expected.
Remember that data types are not to be confused with the two types of data together referred to as customer data — entity data and event data.
A good understanding of data types is required to properly define event properties and entity properties. A well-defined tracking plan must contain the data type of every property to ensure data accuracy and prevent data loss.
Before jumping into the importance of data types, let’s take a look at some of the common data types.
Common Data Types
- Integer (int): It is the most common numeric data type used to store numbers without a fractional component (-707, 0, 707).
- Floating Point Numbers (float): It is also a numeric data type used to store numbers that may have a fractional component like monetary values do (707.07, 0.7, 707.00). Remember that number is often used as a data type that includes both int and float types.
- Character (char): It is used to store a single letter, digit, punctuation mark, symbol, or blank space.
- String: It is a sequence of characters and the most commonly used data type to store text. Additionally, a string can also include digits and symbols, however, it is always treated as text. A phone number is usually stored as a string (+1-999-666-3333) but can also be stored as an integer (9996663333).
- Boolean: It represents the values true and false. When working with the boolean data type, it is helpful to keep in mind that sometimes a boolean value is also represented as 0 (for false) and 1 (for true).
- Enumerated type (enum): It contains a small set of predefined unique values (also known as elements or enumerators) that can be compared and assigned to a variable of enumerated data type. The values of an enumerated type can be text-based or numerical. In fact, the boolean data type is a pre-defined enumeration of the values true and false. For example, if rock and jazz are the enumerators, an enumerated type variable genre can be assigned either of the two values, but not both. Assuming that you are asked to fill in your preferences on a music app and are asked to choose either one of the two genres via a dropdown menu, the variable genre will store either rock or jazz. With enumerated type, values can be stored and retrieved as numeric indices (0, 1, 2) or strings.
- Array: Also known as a list, an array is a data type that stores a number of them in a specific order, typically all of the same types. Since an array stores multiple elements or values, the structure of data stored by an array is referred to as an array data structure. Each element of an array can be retrieved using an integer index (0, 1, 2,…), and the total number of elements in an array represents the length of an array. For example, an array variable genre can store one or more of the elements rock, jazz, and blues. The indices of the three values are 0 (rock), 1 (jazz), and 2 (blues), and the length of the array is 3 (since it contains three elements). Continuing on the example of the music app, if you are asked to choose one or more of the three genres and you happen to like all three (cheers to that), the variable genre will store all three elements (rock, jazz, blues).
- Date: Needs no explanation; typically stores date in the YYYY-MM-DD format (ISO 8601 syntax).
- Time: Stores a time in the hh:mm:ss format. Besides the time of the day, it can also be used to store the time elapsed or the time interval between two events which could be more than 24 hours. For example, the time elapsed since an event took place could be 72+ hours (72:00:59).
- Datetime: Stores a value containing both date and time together in the YYYY-MM-DD hh:mm:ss format.
- Timestamp: Typically represented in Unix time, a timestamp represents the number of seconds that have elapsed since midnight (00:00:00 UTC), 1st January 1970. It is typically used by computer systems to log the precise date and time of an event, down to the number of seconds, in a format that is unaffected by time zones. Therefore unlike datetime, the timestamp remains the same irrespective of your geographical location.
Types of Data Type
In a broad sense, there are three types of data types −
- Fundamental data types − These are the predefined data types that are used by the programmer directly to store only one value as per requirement, i.e., integer type, character type, or floating type. For example − int, char, float, etc.
- Derived data types − These data types are derived using built-in data types which are designed by the programmer to store multiple values of the same type as per their requirement. For example − Array, Pointer, function, list, etc.
- User-defined data types − These data types are derived using built-in data types which are wrapped into a single data type to store multiple values of either the same type or a different type or both as per the requirement. For example − Class, Structure, etc.
Data Types Supported by C++
The following table lists the data types supported by C++
|char||1 byte||-128 to 127 or 0 to 255|
|unsigned char||1 byte||0 to 255|
|signed char||1 byte||-128 to 127|
|int||4 bytes||-2147483648 to 2147483647|
|unsigned int||4 bytes||0 to 4294967295|
|signed int||4 bytes||-2147483648 to 2147483647|
|short int||2 bytes||-32768 to 32767|
|unsigned short int||2 bytes||0 to 65,535|
|signed short int||2 bytes||-32768 to 32767|
|long int||4 bytes||-2,147,483,648 to 2,147,483,647|
|signed long int||4 bytes||-2,147,483,648 to 2,147,483,647|
|unsigned long int||4 bytes||0 to 4,294,967,295|
|float||4 bytes||+/- 3.4e +/- 38 (~7 digits)|
|double||8 bytes||+/- 1.7e +/- 308 (~15 digits)|
|long double||8 bytes||+/- 1.7e +/- 308 (~15 digits)|
Data Types Supported by Java
The following data types are supported by Java −
|byte||1 byte||-128 to 127|
|char||2 byte||0 to 65,536|
|short||2 byte||-32,7688 to 32,767|
|int||4 byte||-2,147,483,648 to 2,147,483,647|
|long||8 byte||-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|float||4 byte||-2147483648 to 2147483647|
|Boolean||1 bit||True or False|
Advantages of Data Type
Following are some of the advantages of using data type in a programming language.
- Data type defines the size of a variable in memory.
- Data type saves memory from unused memory.
- It is used to store various types of data using data type.
- Can create a user-defined data type using standard data type.
- What is a data type?
- What is meant by strongly typed programming language?
- What is meant by weakly typed programming language?
- What is the difference between int and float data types?
- What is the difference between char and string data types?
- What is the difference between array and enumerated data types?
What is a data type?
A data type is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error.
What is the purpose of data type?
A data type constrains the possible values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.
What are Primitive Data Types?
Primitive data types are a set of basic data types from which all other data types are constructed. Specifically, it often refers to the limited set of data representations in use by a particular processor, which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary. More generally, “primitive data types” may refer to the standard data types built into a programming language.
What are Non-Primitive Data Types?
Non Primitive data types are a set of data types that are constructed from primitive data types. These data types are also known as Derived data types.
A data type is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error. It defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.