In programming and computer science, data types define the kind of data a variable can hold, how much memory it uses, and what operations can be performed on it. They ensure efficient storage and prevent errors (e.g., you can't add a string to a number without conversion). Data types are categorized into primitive (basic, built-in) and composite (complex, like arrays or objects).
Different languages have variations, but here's a comparison of common data types across popular languages like Python, Java, and C++.
Key Concepts
Data Type | Description | Example Value | Python | Java | C++ |
|---|---|---|---|---|---|
Integer (int) | Whole numbers (positive, negative, or zero). No decimal points. | 42 or -7 | int (unlimited size) | int (32-bit) | int (32-bit) |
Float (floating-point) | Decimal numbers for precision. | 3.14 or -2.5 | float | double (64-bit) | double (64-bit) |
String (str) | Text or sequences of characters. | "Hello" | str | String (class) | std::string |
Boolean (bool) | True or false values for logic. | True or False | bool | boolean | bool |
Character (char) | Single characters (often a subtype of string). | 'A' | Not primitive (use str[0]) | char (16-bit Unicode) | char (8-bit ASCII) |
Array/List | Ordered collection of items (composite). | [1, 2, 3] | list | Array or List<T> | std::vector<T> |
Dictionary/Map | Key-value pairs (composite). | {"key": "value"} | dict | HashMap<K,V> | std::map<K,V> |
- Static vs. Dynamic Typing: Languages like Java (static) require declaring types upfront (e.g., int x = 5;). Python (dynamic) infers them (e.g., x = 5).
- Type Conversion: Explicit (e.g., int("5") in Python) or implicit (automatic).
- Memory Usage: Integers might use 4 bytes in C++, but Python handles arbitrary size.