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
- Common Types
- Representation in Binary
- Type Sizes and Ranges
- Relationship to Data Structures
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 ( numbers). Multiple bytes are used for larger integer ranges. - 1
integer
occupied 4 bytes = 32 bits, represent 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 | 0 | ||
short |
2 bytes | 0 | |||
int |
4 bytes | 0 | |||
long |
8 bytes | 0 | |||
floating point number | float |
4 bytes | 0.0f | ||
double |
8 bytes | 0.0 | |||
character | char |
2 bytes | 0 | ||
boolean | bool |
1 byte | 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]
javascript code:
const array = [0, 0.0, 'a', false];
typescript code:
const numbers: number[] = [];
const characters: string[] = [];
const bools: boolean[] = [];
Top comments (0)