Casting is a process of changing one type value to another type. In Java, we can cast one type of value to another type. It is known as type casting.
When you assign value of one data type to another, the two types might not be compatible with each other.
If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion.
If not then they need to be casted or converted explicitly known as Narrowing Casting.
=== There are two types of casting in Java : ===
1. Widening Casting (Automatically / Implicit) :
– This involves the conversion of a smaller data type to the larger type size.
byte - short - int - long - float - double
2. Narrowing Casting (Manually / Explicit) :
– This involves converting a larger data type to a smaller size type.
double - float - long - int - short - byte
If the string variable cannot be converted into the integer variable then an exception named NumberFormatException occurs.
#LearningandTeachingCoding