Typecasting refers to the process of converting one data type into another. Since Java is a statically-typed language, the type of each variable is defined at the time of declaration. There are two main types of typecasting in Java:
- Implicit Typecasting (Widening Casting)
- Explicit Typecasting (Narrowing Casting)
Implicit Typecasting
Implicit typecasting occurs automatically when a smaller data type is converted into a larger one. This happens when there is no risk of losing information or precision. For example, converting from an int
to a long
or from a float
to a double
works without issues, as the larger data type can comfortably hold the value of the smaller type.
Syntax
1 |
larger_data_type variable_name = smaller_data_type; |
Java automatically performs widening typecasting when necessary.
Example
1 2 3 4 5 6 7 8 9 |
public class ImplicitTypeCasting { public static void main(String[] args) { int num1 = 100; long num2 = num1; // Implicit typecasting (int to long) System.out.println("num1 (int): " + num1); System.out.println("num2 (long): " + num2); } } |
Output
1 2 |
num1 (int): 100 num2 (long): 100 |
In this example, the int
variable num1
is automatically converted into a long
type and assigned to num2
without needing any explicit casting.
Explicit Typecasting
Explicit typecasting is when you manually convert a larger data type to a smaller one. This can sometimes result in a loss of data because the smaller data type may not be able to hold the full value of the larger type. For instance, converting a double
to an int
will discard the decimal part.
In these cases, you must use a cast operator to explicitly perform the conversion.
Syntax
1 |
smaller_data_type variable_name = (smaller_data_type) larger_data_type; |
Example
1 2 3 4 5 6 7 8 9 |
public class ExplicitTypeCasting { public static void main(String[] args) { double num1 = 45.67; int num2 = (int) num1; // Explicit typecasting (double to int) System.out.println("num1 (double): " + num1); System.out.println("num2 (int): " + num2); } } |
Output
1 2 |
num1 (double): 45.67 num2 (int): 45 |
In this example, the double
value 45.67
is explicitly cast to an int
, and the decimal part is lost.
Differences between Implicit and Explicit Typecasting
Feature | Implicit Typecasting (Widening) | Explicit Typecasting (Narrowing) |
---|---|---|
Definition | Automatically performed by the compiler when converting a smaller type to a larger type. | Manually performed by the programmer when converting a larger type to a smaller type. |
Process | Happens automatically without any explicit instruction. | Requires the programmer to use a cast operator (type). |
Data Loss | No data loss, as the smaller type fits within the larger one. | Data loss may occur because the smaller type cannot fully represent the larger value. |
Example Conversion | int to long, float to double. | double to int, long to short. |
Example Code | long num2 = num1; (where num1 is an int ). |
int num2 = (int) num1; (where num1 is a double ). |
Compiler Involvement | The compiler automatically handles the conversion. | The programmer must manually specify the cast. |
Syntax | No cast syntax required. | Uses cast syntax: (type) variable . |
Type Compatibility | Safe conversion, as the larger type can hold the smaller type. | Potential for data loss if the larger type has a value outside the range of the smaller type. |
Example (No Data Loss) | int num = 100; double result = num; |
double num = 45.67; int result = (int) num; (decimal part lost) |