Type casting in Java is the process of converting one data type into another. It is commonly used when working with different numeric types or when narrowing or widening values.
Java supports two main types of type casting:
// Widening casting example
int number = 10;
double result = number;
System.out.println(result);
// Narrowing casting example
double price = 99.99;
int roundedPrice = (int) price;
System.out.println(roundedPrice);
The first example converts an int into a double automatically. The second example converts a double into an int manually, which removes the decimal part.
long value into an intchar to an int and observe the ASCII value