May 6, 2022

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.

Type casting Java syntax

Syntax for type casting in Java is as following-

(type)value;

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.

double d = 16.78;
int a = (int)d;

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.

int i = 34;
double d = i;
System.out.println("value of d = " + d);
Output
value of d = 34.0

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.

class Parent {
  public void method1() {
    System.out.println("In Parent method");
  }
}

class Child extends Parent {
  public void method1() {
    System.out.println("In Child method");
  }
}

public class Test {
  public static void main(String[] args) {
    Parent p;
    Child c = new Child();
    // Parent reference holding the reference of child object
    p = c;
  }
}

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).

double d = 16.78;
int i = d; // Compile time error

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

double d = 16.78;
int i = (int)d;
System.out.println("value of i = " + i);
Output
value of i = 16

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.

public class Test {
  public static void main(String[] args) {
    Parent p = new Parent();
    Child c;
    c = p;// compile time error
  }
}

class Parent {
  public void method1() {
    System.out.println("In Parent method");
  }
}

class Child extends Parent {
  public void method1() {
    System.out.println("In Child method");
  }
}

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

Parent p = new Parent();
Child c;
c = (Child)p;

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-

class Parent {
  public void method1() {
    System.out.println("In Parent method");
  }
}

class Child extends Parent {
  public void method1() {
    System.out.println("In Child method");
  }

  public void display() {
    System.out.println("in display method of Child class");
  }
}

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

public class Test {
  public static void main(String[] args) {
    Parent p = new Child();
    p.display(); // compile time error
  }
}

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.

public class Test {
  public static void main(String[] args) {
    Parent p = new Child();
    ((Child)p).display();
  }
}
Output
in display method of Child class
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

No comments:

Post a Comment