Type Conversion in Java | Types of Type Casting in Java

1. Objective

In our previous tutorial, we talked about the Switch Statement in Java. Here, in this Java tutorial, we are going to study about the type conversion in Java with the help of various examples. We are also going to study the different types of type casting in Java with an example that are available to us.

So, let us start Type Conversion in Java.

What is Type conversion in Java | Types of Type casting in Java

What is Type conversion in Java | Types of Type casting in Java

2. 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 to 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.

Let’s Read Declaration and Calling a Java Method in detail

3. Types of Type Casting in Java

Here, we discuss 4 types of Type casting in Java, let’s discuss them one by one:

a. Widening or Automatic Type Conversion in Java

Widening conversion happens when two information composes are consequently changed over. This happens when:

  • The two information composes are perfect.
  • When we allow estimation of a littler information writes to a greater information compose.

For Example, in java the numeric information composes are good with each other however no programmed change is bolstered by numeric kind to roast or boolean. Additionally, roast and boolean are not good for each other.

Type Conversion in Java

Widening or Automatic Type Conversion in Java

Example –

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

Let’s know about Serialization and Deserialization in Java

b. Narrowing or Explicit Conversion in Java

Type Conversion in Java

Narrowing or Explicit Conversion in Java

Occasionally there is a chance that we need to allow an estimation of bigger information write to a little information compose we perform express compose throwing or narrowing.

This is helpful for contradictory information writes where programmed change is impossible.

Here, target-type determines the coveted kind to change over the predetermined incentive to char to long.

Example –

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

Output-
7: error: incompatible types: possible lossy conversion from int to char
ch = num;
^
1 error
Read about Java Exception Handling in detail 

Explicit Conversion Example –

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.prinln("Double value "+d);
        System.out.println("Long value "+l);
        System.out.println("Int value "+i);
    }
}

c. Type Promotion in Expressions

While assessing statements, the middle of the road esteem may surpass the scope of operands and subsequently, the articulation esteem will be advanced. A few conditions for type advancement are:

  • Java consequently advances every byte, short, or single operand to the int while assessing an articulation.
  • On the off chance that one operand is a long, float or double the entire articulation is elevated too long, buoy or twofold separately.

Example –

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;        
        double result = (f * b) + (i / c) - (d * s);        
        System.out.println("result = " + result);
    }
}

d. Explicit Type Casting in Expressions

While assessing statements, the outcome is naturally refreshed to bigger information sort of the operand. In any case, in the event that we store that outcome in any littler information compose it creates aggregate time blunder, because of which we have to type cast the outcome.

Let’s Explore Java Character Class Methods with Syntax and Examples

Example –

class Test
{
    public static void main(String args[])
    {
        byte b = 50;
              b = (byte)(b * 2);
        System.out.println(b);
    }
}

This was all about Type Conversion in Java Tutorial. Hope you like our explanation on Types of Type Casting with Example.

Java Quiz

4. Conclusion

Hence, in this tutorial, we studied about What is Type conversion in Java and types of type casting in Java: Widening or Automatic Type Conversion in Java, Narrowing or Explicit Conversion in Java, Type Promotion in Expressions, and Explicit Type Casting in Expressions with examples. Furthermore, if you have any query about Type conversion in Java or Type casting in Java feel free to ask in a comment section.

Related Topic- Java Generics Tutorial

For reference 

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.