In this tutorial, we will learn about explicit type casting in Java with examples. As we know, automatic type conversion is very helpful and convenient, but it cannot handle every type conversion.
For example, if we try to assign a double value to an int variable, the Java compiler does not perform this conversion automatically because it is a narrowing primitive conversion that may result in a loss of information, such as precision or range.
To perform such conversions, Java provides explicit type casting, which allows us to convert a value from one type to another when the conversion is permitted but cannot be performed implicitly in that context.
What is Explicit Type Casting in Java?
Explicit type casting in Java is the process of explicitly converting a value from one data type to another using the cast operator (type). It is commonly used to perform narrowing primitive conversions, such as converting a double value into an int value.
Explicit type casting is also used for certain conversions between reference types and other conversions that require an explicit cast.
In this process, the programmer explicitly specifies the target type using the cast operator, rather than relying on implicit type conversion by the compiler.
The following diagram illustrates how to perform narrowing primitive conversion (explicit type casting) in a Java program.
Syntax to Perform Explicit Type Casting in Java
To perform explicit type casting in Java, we use the cast operator. A cast operator is used to explicitly convert a value from one type to another. It can be used for conversions involving both primitive types and reference types.
The general syntax of a cast expression is given below:
(target_type) expression
Where:
- target_type specifies the data type into which the value is to be converted.
- expression may be a variable, a constant, or another expression whose value is to be converted.
Examples of Explicit Type Casting in Java
The following example demonstrates the use of a cast operator to perform explicit type casting in Java.
Example 1: Narrowing Primitive Conversion from double to int
public class DoubleToIntCastExample {
public static void main(String[] args) {
// Declare and initialize a double variable with a fractional value
double originalDouble = 99.99;
// Explicitly cast the double to an int using the (int) cast operator
// Without the (int) prefix, the compiler would throw a compilation error.
int convertedInt = (int) originalDouble;
// Output the results to the console
System.out.println("Original double value: " + originalDouble);
System.out.println("Converted int value: " + convertedInt);
}
}Output:
Original double value: 99.99 Converted int value: 99
In this example, we are converting the value of double type into int type by using the cast operator (int).
- A double is a 64-bit floating-point number, while an int is a 32-bit signed integer.
- Since a double can hold decimal values and a much larger range than an int, Java treats this as a narrowing primitive conversion.
- The compiler refuses to do this automatically to protect you from losing data, so you must explicitly write (int) to tell the compiler you accept the risk.
- When Java converts a floating-point type (float or double) to an integer type (int or long), it performs truncation toward zero.
- It does not round to the nearest whole number. Therefore, the number 99.99 does not round to 100.
- Instead, the entire fractional part (.99) is simply discarded, leaving only the whole number component (99).
Now, observe that we are losing some digits. So, narrowing conversion is not safe. That’s why the Java compiler forces the programmer to use the cast operator when going for narrowing or explicit casting.
Example 2: Converting double to long with Explicit Casting
public class DoubleToLongExample {
public static void main(String[] args) {
double d = 100.9;
long l = (long) d; // Explicit type casting from double to long
System.out.println("Original double value: " + d);
System.out.println("Converted long value: " + l);
}
}Output:
Original double value: 100.9 Converted long value: 100
In this example, we are converting a floating-point type (double or float) to an integer type (long or int), Java performs truncation toward zero.
The explicit cast operator (long) forces the compiler to accept the conversion, but the fractional part (.9) is completely discarded, leaving only the whole number component (100). Therefore, the output is 100 because the fractional part is lost.
Example 3: Rounding a Floating-Point Number to the Nearest Integer
public class FloatRoundingExample {
public static void main(String[] args) {
int x;
float y = 27.6f;
// Adding 0.5 before casting achieves mathematical rounding
x = (int) (y + 0.5f);
System.out.println("Original float value: " + y);
System.out.println("Rounded int value: " + x);
}
}Output:
Original float value: 27.6 Rounded int value: 28
In this example, narrowing casting always truncates decimals (dropping them without rounding), casting 27.6 directly would yield 27.
By adding 0.5f before the explicit cast, 27.6 + 0.5 becomes 28.1. When this result is cast to an int, the truncation drops .1, successfully rounding the number up to 28. Therefore, the output becomes 28.
Example 4: Converting an int to a char (ASCII / Unicode Mapping)
public class IntToCharExample {
public static void main(String[] args) {
int a = 66;
char ch = (char) a; // Explicit type casting from int to char
System.out.println("Integer value: " + a);
System.out.println("Converted char value: " + ch);
}
}Output:
Integer value: 66 Converted char value: B
In this example, we are converting an int value into a char type. When the value of a is converted into a char, ch will store the character ‘B’ because the integer value 66 corresponds to the uppercase letter ‘B’ in the ASCII character set and Unicode (U+0042).
In Java, the char data type is a 16-bit unsigned integral type that represents a UTF-16 code unit. The integer value 66 corresponds to the Unicode code unit U+0042, which represents the uppercase letter ‘B’.
Using the explicit cast operator (char) performs a narrowing primitive conversion, converting the numeric value 66 into its corresponding char value.
Example 5:
public class ExplicitTest {
public static void main(String[] args) {
byte b;
int i = 257;
double d = 323.142;
// Conversion from int to byte
System.out.println("--- Conversion of int to byte ---");
b = (byte) i;
System.out.println("i = " + i + ", b = " + b);
// Note: b becomes 1 due to integer overflow / modulo arithmetic (257 % 256 = 1).
// Conversion from double to int
System.out.println("\n--- Conversion of double to int ---");
i = (int) d;
System.out.println("d = " + d + ", i = " + i);
// Note: Fractional part (.142) is truncated toward zero.
// 3. Conversion from double to byte
System.out.println("\n--- Conversion of double to byte ---");
b = (byte) d;
System.out.println("d = " + d + ", b = " + b);
// Note: First truncates 323.142 to 323, then casts to byte (323 % 256 = 67).
}
}Output:
--- Conversion of int to byte --- i = 257, b = 1 --- Conversion of double to int --- d = 323.142, i = 323 --- Conversion of double to byte --- d = 323.142, b = 67
In this example program, when the value 257 is cast into a byte variable, the output is 1, which is the remainder of the division of 257 by 256 (the range of a byte). When d is converted into an int, its fractional part is lost.
When d is converted into a byte, its fractional part is also lost, and the value is reduced module 256, which is 67 in this case.
Disadvantage of Explicit Type Casting in Java
There are the following disadvantages of using explicit type casting in Java:
- Loss of Information (Overflow/Wrap-around): Explicit type casting may cause the loss of information or data when converting from one data type to another, particularly during narrowing conversions. As seen in the 257 to byte example, discarding higher-order bits completely alters the magnitude and sign of the number.
- Loss of Precision: Some conversions may result in a loss of precision or range, which can affect the accuracy of the resulting value.
- Loss of Fractional Part: When a double value is cast to an int, its fractional part is truncated (discarded), which may change the original value. Java truncates the value toward zero rather than rounding it.
Difference Between Implicit Casting and Explicit Casting in Java
The main differences between implicit casting and explicit casting in Java are as follows:
- Conversion Process: Implicit type conversion is automatically done by the Java compiler when permitted by its conversion rules, whereas explicit type casting is done by the programmer using the cast operator.
- Use of Cast Operator: Explicit type casting requires a cast operator, whereas implicit type conversion does not require any cast operator.
- Loss of Information: Explicit type casting may cause a loss of information, particularly during narrowing conversions. However, implicit type conversion can also result in a loss of precision in certain cases, such as converting an int value to a float.
- Accuracy and Precision: Explicit type casting may result in a loss of accuracy or precision, depending on the conversion. Similarly, implicit type conversion does not always guarantee that the original value will be preserved exactly.
- Safety: Implicit type conversion is safe, but explicit type casting is not safe.





