Open In App

Java Program to Implement Type Casting and Type Conversion

Improve
Improve
Like Article
Like
Save
Share
Report

There are many situations that come when we have to change the functionality of the output as well as the type of output according to the need of the requirements. For e.g. Entered PI value in decimal format should be converted in the decimal format so that value can be used efficiently without any kind of errors and wrong output.

There are two ways we can change the type from one to another.

  1. Type casting
  2. Type conversion

Type Casting means to change one state to another state and is done by the programmer using the cast operator. Type Casting is done during the program design time by the programmer. Typecasting also refers to Narrow Conversion. Because in many cases, We have to Cast large datatype values into smaller datatype values according to the requirement of the operations. We can also convert large datatype values into smaller datatype values that’s why Type Casting called Narrow Casting.

Syntax: () is Cast Operator

RequiredDatatype=(TargetType)Variable

Example 1:

Java




// Java Program to Implement Type Casting of the Datatype
 
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring an Integer datatype
        int a = 3;
 
        // Casting to Large datatype
        double db = (double)a;
 
        // Print and display the casted value
        System.out.println(db);
 
        // Narrow Casting conversion
        int db1 = (int)db;
         
        // Print an display narrow casted value
        System.out.println(db1);
    }
}


Output

3.0
3

Type Conversion is a type conversion that is done by compiler done by a compiler without the intention of the programmer during the compile time. This Conversion sometimes creates an error or incorrect answer if the proper resultant data type does not mention at the end of the multiple expression of the datatype that’s why Type Conversion is less efficient than Type Casting.

The working mechanism of type conversion is as follows:

double > float > long > int > short > byte

Example 1: Error during type conversion

Java




// Java Program to illustrate Type Conversion
 
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables to values
        // with different return type
        long a = 3;
        byte b = 2;
        double c = 2.0;
 
        // Type Conversion
        int final_datatype = (a + b + c);
 
        // Print statement
        System.out.print(final_datatype);
    }
}


Output:

prog.java:9: error: incompatible types: possible lossy conversion from double to int
        int final_datatype = (a + b + c);
                                    ^
1 error

Output explanation:

When you assign the value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion, and if not then they need to be cast or converted explicitly. For example, assigning an int value to a long variable.

Example 2:

Java




// Java Program to illustrate Type Conversion
 
// Importing input output classes
import java.io.*;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables to values
        // but to different data types
        long a = 3;
        byte b = 2;
        double c = 2.0;
 
        // Type Conversion
        // As long and byte data types are converted to
        // double return type
        double final_datatype = (a + b + c);
 
        // Printing the sum of all three initialized values
        System.out.print(final_datatype);
    }
}


Output

7.0


Last Updated : 28 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads