What is Type conversion in Java with Examples?

In Java, type conversion refers to the process of converting a value of one data type to another data type. This is sometimes necessary when performing certain operations or when passing arguments to a method that expects a specific data type. Java provides two types of type conversion: implicit and explicit.

Implicit type conversion, also known as widening conversion, occurs when a value of a smaller data type is automatically converted to a larger data type. This is done by the compiler to prevent data loss. For example, when an int value is assigned to a long variable, the int value is automatically converted to a long value.

int num = 10; long bigNum = num; // implicit conversion from int to long

Explicit type conversion, also known as narrowing conversion, occurs when a value of a larger data type is manually converted to a smaller data type. This is done using a type cast operator. For example, when a long value is assigned to an int variable, the long value must be manually converted to an int value.

long bigNum = 1000000000; int num = (int) bigNum; // explicit conversion from long to int

It's important to note that when performing explicit type conversion, there is a risk of data loss if the value being converted is too large for the target data type. Therefore, it's important to ensure that the data being converted is within the valid range for the target data type.

 
Widening or Automatic Type Conversion
 
 
  • The two data types are compatible.
  • When we assign the value of a smaller data type to a much bigger data type.
 
 

Example:

class Test
{
	public static void main(String[] args)
	{
		int i = 100;
		
		// automatic type conversion
		long l = i;
		
		// automatic type conversion
		float f = l;
		System.out.println("Int value "+i);
		System.out.println("Long value "+l);
		System.out.println("Float value "+f);
	}
}

Output:

Int value 100
Long value 100
Float value 100.0

Narrowing or Explicit Conversion

 
  • This is useful for incompatible data types where automatic conversion can't be done.
  • Here, the target type specifies the required type to convert the specified value to.
 
//Java program to illustrate incompatible data
// type for explicit type conversion
public class Test
{
public static void main(String[] argv)
{
	char ch = 'c';
	int num = 88;
	ch = num;
}
}

Error:

7: error: incompatible types: possible lossy conversion from int to char
    ch = num;
         ^
1 error

How to do Explicit Conversion?

Example:

//Java program to illustrate explicit type conversion
class Test
{
	public static void main(String[] args)
	{
		double d = 100.04;
		
		//explicit type casting
		long l = (long)d;
		
		//explicit type casting
		int i = (int)l;
		System.out.println("Double value "+d);
		
		//fractional part lost
		System.out.println("Long value "+l);
		
		//fractional part lost
		System.out.println("Int value "+i);
	}
}

Output:

Double value 100.04
Long value 100
Int value 100
 
Example:
//Java program to illustrate Conversion of int and double to byte
class Test
{
	public static void main(String args[])
	{
		byte b;
		int i = 257;
		double d = 323.142;
		System.out.println("Conversion of int to byte.");
		
		//i%256
		b = (byte) i;
		System.out.println("i = " + i + " b = " + b);
		System.out.println("\nConversion of double to byte.");
		
		//d%256
		b = (byte) d;
		System.out.println("d = " + d + " b= " + b);
	}
}

Output:

Conversion of int to byte.
i = 257 b = 1

Conversion of double to byte.
d = 323.142 b = 67

Type promotion in Expressions

While evaluating expressions, the intermediate value may exceed the range of operands and hence the expression value are going to be promoted. Some conditions for type promotion are:

  1. Java automatically promotes each byte, short, or char operand to int when evaluating an expression.
  2. If one operand may be a long, float or double the entire expression is promoted to long, float or double respectively.

Example:

//Java program to illustrate Type promotion in Expressions
class Test
{
	public static void main(String args[])
	{
		byte b = 42;
		char c = 'a';
		short s = 1024;
		int i = 50000;
		float f = 5.67f;
		double d = .1234;
		
		// The Expression
		double result = (f * b) + (i / c) - (d * s);
		
		//Result after all the promotions are done
		System.out.println("result = " + result);
	}
}

Output:

Result = 626.7784146484375

Explicit type casting in Expressions

 

Example:

 
//Java program to illustrate type casting int to byte
class Test
{
	public static void main(String args[])
	{
		byte b = 50;
		
		//type casting int to byte
		b = (byte)(b * 2);
		System.out.println(b);
	}
}

Output

 100

Submit Your Programming Assignment Details