Type Casting in Java

Type Casting in Java with Examples

In this article, I am going to discuss Type Casting in Java with Examples. Before learning Type Casting, you must know about Java Data Types. So, please read our previous article, where we discussed Literals in Java with Examples. At the end of this article, you will understand what Type Casting is and why and when we need Type Casting in Java.  

What is Type Casting in Java?

The process of converting the value of one data type (int, float, double, etc.) into another data type is known as Type Casting. Type Casting is the temporary conversion of a variable from its original data type to some other data type, like being cast for a part in a play or movie.

With primitive data types if a cast is necessary from a less inclusive data type to a more inclusive data type it is done automatically. If a cast is necessary from a more inclusive data type to a less inclusive data type the cast must be done explicitly by the programmer.

Conversion between Primitive Data Types: Cast Operator

The Cast Operator manually lets you convert a value of the data type into another data type. It can be done by Writing the cast operator in front of the expression that needs to be converted.

Syntax : (dataTypeName) expression;

When casting from a double to an int, the fractional part will be discarded.

Example :

int x;
double y = 2.5;
x = (int)y;

Here, int is the Cast Operator.

Java compiles the code with the cast operator with no problems. In this case, the variable y has a value of 2.5 (the floating-point value) which must be converted to an integer. The value that is returned and stored in variable x would be truncated, which means the fractional part of the number is lost to accommodate the integer data type. Thus, x=2 and, the value of the variable y is not changed at all: y=2.5

Types of Type Casting in Java:
  1. Widening or Automatic Type Casting
  2. Narrowing or Explicit type Casting
Widening Type Casting in Java:

Widening Type Casting is also known as Automatic Type Casting. When you assign the value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion. For better understanding, please have a look at the following image.

Type Casting in Java with Examples

It happens when the two data types are compatible and when we assign the value of a smaller data type to a bigger data type.

Example of Widening or Automatic Type Casting in Java:
package Demo;
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 ("Float Value is : " + floatVariable);
        System.out.println ("Long Value is : " + longVariable);
    }
}
Output:

Example of Widening or Automatic Type Casting in Java

Narrowing Type Casting in Java:

Narrowing Type Casting is also known as Explicit type Casting. This is useful for incompatible data types where automatic conversion cannot be done. It happens when the two data types are not compatible and when we want to assign a value of a larger data type to a smaller data type.

Example of Narrowing or Explicit Type Casting in Java:
package Demo;
public class ExplicitTypeCasting
{
    public static void main (String[]args)
    {
        double doubleVariable = 100.04;
        long longVariable = (long) doubleVariable;
        int intVariable = (int) longVariable;
        System.out.println ("Double Value is : " + doubleVariable);
        System.out.println ("Long Value is : " + longVariable);
        System.out.println ("Integer Value is : " + intVariable);
    }
}
Output:

Example of Narrowing or Explicit Type Casting in Java

In the next article, I am going to discuss Operators in Java with Examples. Here, in this article, I try to explain Type Casting in Java with Examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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