Explain the difference between dynamic and static typing?
**Static Typing** and **Dynamic Typing** refer to how a programming language handles variable types.
1. **Static Typing**:
- **Definition**: The type of a variable is determined at compile time and does not change during program execution.
- **Example Languages**: C, C++, Java.
- **Advantages**: Errors related to type mismatches are caught early, which can improve performance and code reliability.
- **Example**:
```java
int number = 5; // 'number' is explicitly typed as an integer.
```
2. **Dynamic Typing**:
- **Definition**: The type of a variable is determined at runtime and can change during program execution.
- **Example Languages**: Python, JavaScript, Ruby.
- **Advantages**: Greater flexibility and ease of use, especially for scripting and rapid development.
- **Example**:
```python
number = 5 # 'number' can be assigned any type, e.g., integer or string.
number = "Hello" # Now 'number' is a string.
```
In summary, static typing enforces type constraints at compile time, while dynamic typing does so at runtime, impacting flexibility and error checking.