Data Types
Introduction to Data Types
- Data types define the kind of value a variable can store and how it’s stored.
- Different data types have different sizes and can store different ranges of values.
Common Data Types
- Integer: Used to store whole numbers.
- Floating Point: Used for numbers with decimal points.
- Boolean: Represents either ‘True’ or ‘False’.
- Character: For single letters or symbols.
- String: Used to store text.
Signed and Unsigned Integers
- Signed Integers represent both positive and negative numbers using most significant bit (MSB) for sign (0 for positive, 1 for negative).
- Unsigned Integers only represent non-negative values and thus have a larger positive range.
Limits of Data Types
- Each data type has a specific range of values it can store.
- For instance, an 8-bit signed integer can store values from -128 to 127.
Choosing the Right Data Type
- The choice of a data type comes down to the requirements of the data you’re dealing with.
- Using the wrong data type might lead to unexpected results or wastage of memory.
Data Type Conversions
- It’s possible to change the data type of a value, this is known as type conversion or type casting.
- Some type conversions can be done automatically by the system (implicit conversion), while others need to be instructed manually (explicit conversion).
- Care should be taken as type conversion can also lead to data loss if the receiving type’s range cannot handle the original data.