Type Conversion (casting)
In the Java programming language, data type conversion or type casting is the process of modifying, converting, or updating a programmer-declared default data type value to another data type. The Java development environment allows developers to use both implicit and explicit data type casting or conversion methods, depending on the program’s data type. Understanding this process is crucial for proper understanding and analysis of universal data types in small- to large-scale Java program development and for creating accurate program source code. This process helps developers better understand how and when to apply type conversion methods.

Implicit Java Type Conversion/Widening Conversion.
Implicit type conversion is automatically implemented in Java programs when a Java user manually assigns a small-declared data type variable value to a large data type parameter. This type of conversion is a secure and efficient process in existing Java programs. There is no data loss during the conversion process, as large variables can automatically adjust the existing value of small data type parameters.
Example of implicit conversion in Java.
If we convert a previously declared int (32-bit) data type variable to a long (64-bit) data type variable in a Java program, the conversion will be done automatically without any explicit data type casting.
public class Main
{
public static void main(String[] args) {
int intNumber = 289;
long longNumber = intNumber; // This is an implicit conversion from an int 32-bit data type to a long 64-bit data type.
System.out.println(longNumber); // Type Conversion Output – 289
}
}
Implicit Java Type Conversion Explanation.
- In this program, the int data type value is automatically converted and promoted to the long data type, and the updated data type casting value is displayed.
Rules for Java Implicit Conversion.
Remember, data type conversion or casting in a Java program is only possible by converting from a small data type variable declaration to a large data type.
For example, byte → short → int → long → float → double data type.
In this data type conversion process, the range of the destination data type must be larger than the source data type. Because of this, no data loss or issues arise during the data type conversion process.
Implicit Java Data Type Conversion/Narrowing Conversion.
Explicit data type conversion is a method used in Java programming, also known as narrowing conversion. Narrowing conversion is performed when a Java user attempts to assign a large data type variable to a small data type parameter. In such a situation, the Java program does not perform the conversion or casting automatically. To perform such data type conversion, the Java programmer must explicitly tell the compiler or indicate the use of casting. This is because the large data type variable cannot be converted or fitted into a small data type value, which can sometimes result in loss of data.
Example of Java Explicit Data Type Conversion.
In Java programming, when programmers convert a variable value from a double (64-bit) data type to a small int (32-bit) data type, explicit casting is applied to complete this process. In Java programming, a double data type variable holds decimal values, while an int data type variable can only hold and process integer values.
public class Main
{
public static void main(String[] args) {
double doubleInteger = 208.37;
int intInteger = (int) doubleInteger ; // Explicit data type casting from double to int variable here
System.out.println(intInteger ); // double to int data type conversion output is – 208
}
}
Explicit Java Type Conversion Explanation.
- This Java program removes the decimal part of the data type value 208.37, and displays the result as the complete integer value 208 with the fractional part removed.
Syntax for Explicit Java Casting.
targetdataType variableName = (targetdataType) sourcedatatypeValue;
In this example.
- the above program uses (int) as the data type variable, which explicitly converts the double data type to the int data type.
Use cases for Explicit Conversion in Java.
- Double to Float Data Type – In Java programs, the conversion of double to float data type requires more accuracy than the conversion of double to float data type. Therefore, casting is applied to make the data type conversion smaller.
- Long to int Data Type – In Java programs, the long data value declared can be larger than the maximum value of the int data type, hence data type casting is necessary.
- Int to Byte Data Type – In Java programs, the value of an int data type variable can exceed the range of a byte data type variable. Therefore, the Java developer must indicate this to the Java compiler by using explicit casting.
