Automatic type conversion

 Automatic type conversion

Automatic type conversion in Java, also known as implicit type casting or widening conversion, is a feature where the Java compiler automatically converts one data type to another. This usually occurs when you assign a value of one type to a variable of another type, and the conversion can be done safely without data loss.

Widening Conversion

Widening conversion happens when a smaller data type is converted to a larger data type. This type of conversion is safe and doesn't lose information. The following table shows the order of data types from smaller to larger:

  • byte → short → int → long → float → double

Example


In the red box, call the "add" method with two integers. The integer value should be received by another red box method, which automatically converts into a double, and the output should be 5.0.

Automatic data type conversion list






















Comments