Cast Operator in Java
A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators.
Type casting in Java is a technique or procedure that transforms a type of data into another type of data both manually and also automatically. The developer does manual translation, whereas the compiler performs the automated transformation.
The types of type casting
Type casting comes in two types:
1. Widening Type casting
2. Narrowing Type Casting
Widening
Widening type casting refers to transforming a low information type into a higher one. Casting down and implied conversion are other names for it. It happens on its own. It is secure since there is no possibility of data loss.
The event occurs when:
- The two data kinds need to work together.
- A greater target type than that of a smaller source type is required.
Byte, Short, Char, Int, Long, Float, Double
For instance, there is no automated translation from a numeric data form to a char or boolean data type. Additionally, the data types char and boolean are incompatible with one another.
Example: WidiningExample.java
//this program is for type conversions in java
//importing the packages
import java.io.*;
import java.util.*;
public class WidiningExample
{
public static void main(String[] args)
{
int n1 = 35;
// the data is converted automatically
long n2 = n1;
// the long data type is easily converted to float
float n3 = n2;
System.out.println("The value of integer before conversion: "+n1);
System.out.println("The value of long after the conversion from int: "+n2);
System.out.println("The value of float after conversion form long: ”+n3);
}
}
Output
The value of integer before conversion: 35
The value of long after the conversion from int: 35
The value of float after conversion form long: 35.0
In the example shown, a variable named n1 was changed into a long type. The long type is then changed into the float type.
Narrowing
Narrowing type casting refers to transforming a higher data type into the lesser one. Casting up and explicit translation are other names for it. The developer does it by hand. The compiler will raise a compile-time warning if the casting is not done.
double, float, long, int, char, short, and byte are all data types.
Here is an illustration of thin-type casting.
The narrower type casting has been done twice in the case below. First, the double data type was transformed into a long data type, and then the long data type was changed to an int type.
Example: NarrowingExample.java
//this program is for type conversions in java
//importing the packages
import java.io.*;
import java.util.*;
public class NarrowingExample
{
public static void main(String args[])
{
double d1 = 164.65;
//switching from double to long data type
long l1 = (long)d1;
//long data type conversion to the int data type
int i1 = (int)l1;
System.out.println("The double value before conversion: "+d1);
// removing the fractional part
System.out.println("The value of long data type after conversion from double: "+l1);
//removing the fractional part
System.out.println("The value of int after converting from long: "+i1);
}
}
Output
The double value before conversion: 164.65
The value of long data type after conversion from double: 164
The value of int after converting from long: 164