DEV Community

Cover image for Basic Data Types
Đặng Đình Sáng
Đặng Đình Sáng

Posted on

Basic Data Types

Basic data types are the fundamental building blocks CPUs use to represent numeric values, text, pictures, videos, speech, 3D models, and more. Although these data are organized differently, they are composed of various basic data types. They form the lowest level of data that programming languages can directly operate on.

Table Of Contents

A real-life example

The Shipment Journey

Every package is assigned a unique tracking number for identification. As this number can be long, companies use integer data types like long to store billions of IDs without size limitations.

The carrier provides estimated delivery dates, sometimes with partial day granularity. Dates contain both date and time info, making floating point data types suitable to represent complex timestamps down to the minute.

Status Updates

As your package travels, the system logs status messages like "Left Sort Facility" at each location. These short text updates are easily stored using string data types.

Heavier items require special handling and are subject to additional fees. To track weights precisely, the float data type captures decimal values like "4.56 lbs".

Delivery Details

Your driver marks the package as delivered by getting a signature. Boolean data types store true/false values to flag a delivery as complete.

Different service speeds offer varying delivery promises. Enumeration string types classify options as "Standard", "Expedited" etc.

Data types serve many purposes in real life. How it can be stored on the computer?

Common Types

Basic data types are types that the CPU can directly perform operations

  • Integer types (byte, short, int, long) for whole numbers
  • Floating point types (float, double) for decimal numbers
  • Character type (char) for single-character text
  • Boolean type (bool) for true/false values

Representation in Binary

Basic data types are stored in computers in binary form

Inside computers, all data is stored as bits - the basic units of 1s and 0s. Data types determine how many bits are used to represent each value.

For example:

  • 1 byte typically uses 8 bits and can store values from 0 to 255 ( 282^8 numbers). Multiple bytes are used for larger integer ranges.
  • 1 integer occupied 4 bytes = 32 bits, represent 2322^{32} numbers.

Type Sizes and Ranges

The number of bits allocated for a type determines its possible value range. Larger types can hold bigger numbers but take up more memory. Value ranges also depend on the programming language.

For illustration, I've included a table showing common type sizes and value ranges for the Java programming language. These serve as a general guide but can vary between languages and platforms.

type symbol space minimum maximum default
integer byte 1 byte 27-2^7 2712^7 - 1 0
short 2 bytes 215-2^{15} 21512^{15} - 1 0
int 4 bytes 231-2^{31} 23112^{31} - 1 0
long 8 bytes 263-2^{63} 26312^{63} - 1 0
floating point number float 4 bytes 1.17510381.175 * 10^{-38} 3.40310383.403 * 10^{38} 0.0f
double 8 bytes 2.225103082.225 * 10^{-308} 1.798103081.798 * 10^{308} 0.0
character char 2 bytes 00 21612^{16} - 1 0
boolean bool 1 byte falsefalse truetrue false

Relationship to Data Structures

Data types indicate the type of primitive content, while data structures define how that content is organized in memory. We can store different data types like integers, floats, and chars using the same structures as arrays.

Structures provide the logical “container” for values, independent of their basic format. This relationship allows flexibility in solving problems by decoupling logical structure from physical representation.

python code:

numbers: list[int] = [0] * 2
decimals: list[float] = [0.0] * 2
characters: list[str] = ['0'] * 2
bools: list[bool] = [False] * 2
data = [1, 0.1, '0', False]
Enter fullscreen mode Exit fullscreen mode

javascript code:

const array = [0, 0.0, 'a', false];
Enter fullscreen mode Exit fullscreen mode

typescript code:

const numbers: number[] = [];
const characters: string[] = [];
const bools: boolean[] = [];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)