Type Casting And Type Conversion in Java

When you assign a value of one type to a variable of another data type, Java performs type conversion to accommodate the value to different type. This type conversion may happen automatically or you may need an explicit type casting to perform the type conversion.

So, both of these terms “type conversion” and “type casting” are related to conversion of one data type to another in Java.

  • Type Conversion– When the two types are compatible with each other, then conversion of one type to another is done automatically by the compiler. For example assigning an int value to float variable.
  • Type Casting– If the types are incompatible then automatic type conversion is not possible. If you want conversion between incompatible types then you will have to do explicit type casting.

Syntax for type casting in Java is as following-

For example if you want to assign a double value to an int variable that conversion from double to int won’t happen automatically. You need to type cast the double to int for that conversion.

For example-

Types of type conversions in Java

Type conversion in Java can be classified into two types-

1- Widening conversion– If types are compatible and destination type is larger than the source type then the widening type conversion happens.
For example, in case of primitive numeric data types the widening of type can be shown as follows-

byte – short – int – long – float – double

Widening conversion can be further divided into-

  • Widening primitive type conversion
  • Widening reference type conversion

Widening primitive type conversion

For primitive data types widening type conversion happens automatically.

Output

Widening reference type conversion

In a parent-child class relationship, a reference of parent type can hold child type reference without requiring any explicit type casting.

For example-

If there is a class called Parent which is extended by class called Child. Then an instance of type Child can be stored in Parent type reference with out any need of type casting as it is a Widening reference type conversion.

2- Narrowing conversion– If destination type is narrower than the source type then the narrowing type conversion happens. Narrowing type conversion won’t happen automatically, you need to explicitly narrow the type by using type casting.

Narrowing conversion can be further divided into-

  • Narrowing primitive type conversion
  • Narrowing reference type conversion

Narrowing primitive type conversion

For primitive data types if the destination type is smaller than the source type then it’s a narrowing type conversion and this type conversion doesn’t happen automatically.

For example the following lines of code result in compile time error- “Type mismatch: cannot convert from double to int” as int (source type) is narrower than double (target type).

In the case of narrowing type conversion you need type casting to do the conversion.

Output

As you can see value is also truncated while casting from double to int as int doesn’t have fractional part.

Narrowing reference type conversion

If you want a child type (sub class) reference to hold a reference of Parent (Super class) that won’t happen automatically.

The following Java program throws compile time error “Type mismatch: cannot convert from Parent to Child” as Parent is super class and it is assigned to a subclass instance.

In order to do this narrowing type conversion you will need to type cast the reference.

Type casting in Java to narrow down the reference is needed if your child class has methods of its own, apart from the methods of the super class it overrides. Consider the scenario as follows-

Here class Child overrides the method1() method of the super class and it also has a method display() of its own.

Trying to call display method with a super class reference won’t work and you will get a compiler error “display cannot be resolved or is not a field”.

To be able to call display() method you’d need type casting.

Output

That’s all for the topic Type Casting And Type Conversion in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.