Type Conversion in Java – Explore Automatic & Explicit Type Casting

Before we start our article, you should know type casting and type conversion in Java is the same thing. You can use the terms them interchangeably.

Java Type Conversion is the process of assigning a value from one data type to another. There are two types of type casting in Java – Automatic and Explicit. Let’s explore every detail about type conversion.

So, before we start our article, I recommend you to check the data types in Java.

1. Type Conversion in Java

When we need to assign the values of one data type to another and there might be a possibility that the two types are not compatible with each other, then Java does the type casting for us if it can be done automatically, and if not we need to do it explicitly.

2. Types of Type Conversion in Java

The following are the two types of type casting in Java.

Type Conversion in Java

2.1 Automatic Type Conversion in Java

Automatic or widening type conversion takes place when two data types are automatically converted. This happens when:

  • The two data types are compatible.
  • When we want to convert a smaller type to the larger type size.

Do you know what are Abstract Data Types in Java?

Automatic Type Conversion in Java

Example –

package com.dataflair.typecasting;

public class AutomaticTypeConversion {
  public static void main(String[] args)
  {
    int intVariable = 100; 
    long longVariable = intVariable;
    float FloatVariable = longVariable;
    System.out.println("Integer value is : "+intVariable);
    System.out.println("Long value is : "+longVariable);
    System.out.println("Float value is : "+FloatVariable);
  }
}

Output-Automatic-Type-Conversion

2.2 Explicit Type Conversion in Java

Explicit or narrowing casting in Java is done manually. When we want to convert a larger type to the smaller type size then we use explicit type casting.

Explicit Type Conversion in Java

Example –

public class Test
{
  public static void main(String[] argv)
  {
    char ch = 'c';
    int num = 88;
    ch = num;
  }
}

Output-

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to char at com.dataflair.typecasting.ExplicitConversion.main(ExplicitConversion.java:8)

Recommended Reading – Singleton Class in Java

Explicit Conversion Example –

Join DataFlair on Telegram
package com.dataflair.typecasting;

public class ExplicitTypeConversion {
 public static void main(String[] args)
{
    double doubleVariable = 120.14;
    //explicit type casting
    long longVariable = (long)doubleVariable;
    //explicit type casting
    int intVariable = (int)longVariable;
    System.out.println("Double value "+doubleVariable);
    System.out.println("Long value "+longVariable);
    System.out.println("Int value "+intVariable);
    }
  }

Output-Explicit-type-conversion

3. Conversion of Primitives

Following are the situations in which conversion of primitive type takes place

3.1 Assignment Conversion

When a variable of one data type is assigned to another datatype then assignment conversion occurs. We can use only widening conversions in the assignment.

Example-

double number1;
int number2 = 10;

number1 = number2; //number 2 is converted to double

Nourish Your Fundamentals with Java Operators

3.2 Arithmetic Promotion

Arithmetic promotions occur when we are working with arithmetic expressions where the operator requires the operands to perform the operation.

Example-

int totalnum =5;
double totalsum = 25;

double average = totalsum / totalnum; //totalnum is also converted to double

3. Method Call

This type of conversion occurs at the time of method call when an argument of a datatype is passed to a method that expects a different datatype.

Example-

float floatVar = 2.18f
double doubleVar = Math.cos(floatVar); //method cos() expects double and hence floatVar is converted to double

Take a deep dive and explore everything about Java Method

Java Quiz

4. Summary

Type casting is the way to change expression from one data type to another. With the help of this, you can convert int to long, byte to short or vice versa. With the help of conversion primitives, you can convert assignment, arithmetic, and methods.

Hope you liked the article. Please share your experience with comments.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.