Object-Oriented Programming (OOP)
4. Introduction to Data Types
In programming, data types define the kind of data a variable can hold and the operations that can be performed on it. They are essential for managing data efficiently in any programming language. Basic data types are the fundamental building blocks, providing the foundation for more complex data structures. This lecture covers the primary data types, their characteristics, use cases, and examples across popular programming languages like Python, Java, and C++.
1. Integer
Integers are whole numbers, positive or negative, including zero, without any fractional or decimal component.
Characteristics
Storage: Typically uses 4 bytes (32 bits) in most languages, though sizes can vary (e.g., 2 bytes for short in Java, 8 bytes for long).
Range: Depends on the language and bit size. For a 32-bit signed integer, the range is approximately -2,147,483,648 to 2,147,483,647.
Operations: Addition (+), subtraction (-), multiplication (*), division (/ or //), modulus (%), etc.
No decimal precision: Integers are exact, making them ideal for counting and indexing.
Use Cases
Counting items (e.g., loop counters, quantities).
Array or list indexing.
Representing discrete values like IDs or scores.
Examples
Python: x = 42, y = -10
Java: int x = 42;
C++: int x = 42;
Notes
In Python, integers have unlimited precision (no fixed size limit).
In Java and C++, integers are fixed-size, and exceeding the range causes overflow unless using larger types like long.