What is Java Typecasting
Typecasting in Java refers to converting a variable from one data type to another. This is often necessary when you’re working with different data types together in a single expression or when you need to conform to a specific method signature. There are two main types of typecasting in Java:
- Widening (Automatic) Typecasting
- Narrowing (Explicit) Typecasting
1. Widening (Automatic) Typecasting
Widening typecasting, also known as implicit typecasting, occurs automatically when you convert a smaller data type to a larger one. This type of conversion is safe because there is no risk of data loss.
Example of Widening Typecasting:
java