A. Primitive Data Types: Primitive data types are the most basic data types in programming languages. They are called "primitive" because they are built into the language and cannot be broken down any further. The most commonly used primitive data types are:
- Integer: It is a whole number without any decimal point. It can be positive, negative or zero. For example, 1, 2, 3, -4, -5, 0.
- Floating point: It is a number with decimal point. It can be positive, negative or zero. For example, 3.14, -2.5, 0.0.
- Boolean: It represents a logical value, which can be either true or false. For example, true, false.
- Character: It represents a single character such as a letter or a digit. For example, 'a', 'b', '1', '2'.
- String: It represents a sequence of characters. For example, "Hello, World!", "123".
Example:
// Integer int age = 25;
// Floating point float price = 3.99;
// Boolean bool isAvailable = true;
// Character char grade = 'A';
// String string name = "John";
B. Variables and Constants: Variables and constants are used to store values in a program. Variables can be changed during the execution of the program, while constants cannot be changed once they are assigned a value.
- Variables: A variable is a named storage location in the computer's memory that can hold a value. To declare a variable, you must specify its data type and name. For example, int age = 25; declares an integer variable named age with a value of 25.
- Constants: A constant is a named value that cannot be changed during the execution of the program. To declare a constant, you must use the const keyword. For example, const float PI = 3.14159; declares a constant named PI with a value of 3.14159.
Example:
// Variable int age = 25;
age = 26;
// Constant
const float PI = 3.14159;
// PI = 3.14; // Error: Cannot assign a new value to a constant
C. Operators and Expressions: Operators are symbols that perform operations on one or more operands. An expression is a combination of variables, constants, and operators that can be evaluated to a value. There are different types of operators in programming languages:
- Arithmetic operators: They are used to perform mathematical operations such as addition, subtraction, multiplication, and division. For example, +, -, *, /.
- Comparison operators: They are used to compare two values and return a Boolean value indicating whether the comparison is true or false. For example, <, >, <=, >=, ==, !=.
- Logical operators: They are used to combine Boolean values and return a Boolean result. For example, && (logical and), || (logical or), ! (logical not).
- Assignment operators: They are used to assign a value to a variable. For example, =, +=, -=, *=, /=.
Example:
// Arithmetic operators
int sum = 1 + 2; // sum = 3
int difference = 5 - 3; // difference = 2
float product = 2.5 * 3; // product = 7.5
float quotient = 10 / 3.0; // quotient = 3.3333
// Comparison operators
bool isGreater = 5 > 3; // isGreater = true
bool isEqual = 5 == 5; // isEqual = true
// Logical operators
bool result = (5 > 3) && (4 < 6); // result = true
bool isTrue = !false; // isTrue = true
// Assignment operators
int a = 10;
a += 5; // a = 15
a *= 2; // a = 30
D. Type Conversion and Casting: Type conversion is the process of converting one data type to another data type. In some cases, it may be necessary to convert a value from one data type to another in order to perform a certain operation. Casting is a type of type conversion that explicitly converts a value from one data type to another.
- Implicit type conversion: It happens automatically by the compiler when two different data types are used in an expression. For example, when an int is added to a float, the int is converted to a float.
- Explicit type conversion: It is done explicitly by the programmer using casting. For example, (int) 3.14; converts the floating-point number 3.14 to an integer.
Example:
// Implicit type conversion
int age = 25; float height = 5.8;
float total = age + height; // Implicit conversion of age to float
// Explicit type conversion (casting)
float price = 3.99;
int roundedPrice = (int) price; // Explicit conversion of price to int
Note: When converting from a larger data type to a smaller data type, there may be a loss of precision or information. Therefore, it is important to use type conversion carefully and appropriately.