Type Conversion in Java

Type Conversion and Casting in Java

Type conversion and casting in Java is a way to change the data type of a value or a variable into another data type. Type conversion is another name of type-casting. It can be demonstrated in the program both by the programmer and the compiler.

There are two types of type-casting in Java:

    • Implicit Type – Casting in Java

    • Explicit Type – Casting in Java

Implicit Type Casting in Java

Implicit Type-Casting in Java takes place automatically in the program by the compiler when a variable of low data type is assigned to a variable of high data type. It is also known as Widening Type Casting or Casting Down. It takes place between data types that are compatible with each other. It does not result in the loss of data.

Example of implicit type casting in java

//Java Program showcasing Implicit Type Casting
public class ImplicitTypeCasting {
    public static void main(String[] args){
        //Variable initialization of type byte
        byte var_byte=23;
        System.out.println("var_byte before conversion: "+var_byte);

        //implicit conversion of byte to short
        short var_short=var_byte;
        System.out.println("var_byte after implicit conversion to short: "+var_short);

        //implicit conversion of byte to int
        int var_int=var_byte;
        System.out.println("var_byte after implicit conversion to int: "+var_int);

        //implicit conversion of byte to long
        long var_long=var_byte;
        System.out.println("var_byte after implicit conversion to long: "+var_long);

        //implicit conversion of byte to float
        float var_float=var_byte;
        System.out.println("var_byte after implicit conversion to float: "+var_float);

        //implicit conversion of byte to double
        double var_double=var_byte;
        System.out.println("var_byte after implicit conversion to double: "+var_double);
    }
}

Output:

var_byte before conversion: 23
var_byte after implicit conversion to short: 23
var_byte after implicit conversion to int: 23
var_byte after implicit conversion to long: 23
var_byte after implicit conversion to float: 23.0
var_byte after implicit conversion to double: 23.0

Note:

Implicit conversion doesn’t happen with mismatched data types.

 

Explicit Type Casting in Java

Explicit Type-Casting in Java is done in the program manually by the programmer. It is the conversion of a high data type into a low data type. It is also known as Narrowing Type-Casting or Casting Up. It results in a loss of data. It is inculcated in the program using the cast operator which is (type).

Example of explicit type casting in java.

//Java Program showcasing Explicit Type Casting

public class ExplicitTypeCasting {
    public static void main(String[] args){
        //Variable initialization of type double
        double var_double=23.0;
        System.out.println("var_double before conversion: "+var_double);

        //explicit conversion of double to float
        float var_float=(float)var_double;
        System.out.println("var_double after explicit conversion to float: "+var_float);

        //explicit conversion of double to long
        long var_long=(long)var_double;
        System.out.println("var_double after explicit conversion to long: "+var_long);

        //explicit conversion of double to int
        int var_int=(int)var_double;
        System.out.println("var_double after explicit conversion to int: "+var_int);

        //explicit conversion of double to short
        short var_short=(short)var_double;
        System.out.println("var_double after explicit conversion to short: "+var_short);

        //explicit conversion of double to byte
        byte var_byte=(byte)var_double;
        System.out.println("var_double after explicit conversion to byte: "+var_byte);
    }
}

Output:

var_double before conversion: 23.0
var_double after explicit conversion to float: 23.0
var_double after explicit conversion to long: 23
var_double after explicit conversion to int: 23
var_double after explicit conversion to short: 23
var_double after explicit conversion to byte: 23

Difference between Implicit and Explicit Type Casting in JAVA

Implicit Type Conversion Explicit Type Conversion
It is the changing of a low data type into a high data type. It is the changing of a high data type into a low data type.
It is done automatically in the program. It is done manually in the program.
It is introduced in the program by the compiler. It is introduced in the program by the programmer.
It is also known as Widening Type Casting. It is also known as Narrowing Type Casting.
Example: byte b=10;

int i=b;

Example: int i=10;

byte b=byte(I);

 

Also read, Variables in Java Programming.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *