HomeJava

Java for Testers – Types of Type Casting

Java for Testers – Types of Type Casting

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained about the Type Casting in Java.

In this article, I am going to explain about the different types of Type Casting in Java.

Java for Testers – Types of Type Casting

As explained in the previous article, Type Casting is nothing but assigning value of one data type to the another. i.e. The cases where the data type of the literal value assigned to the variable is different the data type of the variable.

There are two types of Type Casting:

  1. Widening Casting
  2. Narrowing Casting

I will explain these two types in detail below:

Widening Casting

In Widening Casting, the small size type when assigned to the larger size type is automatically converted.

Example: Assigning int data type to the long data type as shown below:

long a = 123;

In the above example, as int value assigned to the long data type variable is smaller in size, it will be automatically converted and assigned to the long data type by Java.

See the practical demonstration below:

Type Casting Types - widening example

In the below statement, the data types on the left when assigned to all the data types on their right will be automatically converted and assigned by Java:

byte < short < char <  int < long < float < double

As byte data type in the above line is shorter in size than all the data types on its right, byte data type value will be automatically converted to the other bigger size-types by Java.

Narrowing Casting

In Narrowing Casting, the bigger size type when assigned to the smaller size type needs to be manually or explicitly converted to the bigger size type.

Example: Assigning long data type to the int data type as shown below:

int a = (int) 123L;

In the above example, as long type value is bigger in size when compared to the int data type, we have explicitly convert the long type value by using (int) before assigning to the variable. Java by default will not convert, hence we have to explicitly provide (int).

See the practical demonstration below:

Type Casting Types - narrowing example

In the below statement, the data types on the left when assigned to all the data types on their right, needs to be manually or explicitly converted and then assigned to the variable (i.e. Java will not automatically convert)

double < float < long < int < char < short < byte

As double data type in the above line is larger in size than all the data types on its right, on assigning double data type value to the other smaller size-types, we will get a compiler error.

In order to resolve the compiler error, we have to explicitly convert to the required data type before assigning. i.e. On assigning a double data type to the byte data type variable, we have to explicitly convert the double data type value to the byte using (byte) in the statement (i.e. byte a = (byte) 123456.789;)

See the practical demonstration below:

Type Casting Types - narrowing example 2

Here concludes this article on different types of Type Casting in Java.

In the next article, I will explain about different types of operators in Java.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning 🙂

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

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