Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

October 1, 2014 Categories: Core Java Basics. 2 Comments on Java type conversion and casting

In this article you will learn about Java type conversion and casting with appropriate examples. Converting a value from one type to another type (data type) is known as type conversion.

Type conversion is of types based on how the conversion is performed: 1) Implicit conversion (also known as automatic conversion or coercion), 2) Explicit conversion (also known as type casting).

java type conversion and casting


Implicit Conversion or Coercion

This type of conversion is performed automatically by Java due to performance reasons. Implicit conversion is not performed at all times. There are two rules to be satisfied for the conversion to take place. They are:

  1. The source and destination types must be compatible with each other.
  2. The size of the destination type must be larger than the source type.

For example, Java will automatically convert a value of byte into int type in expressions since they are both compatible and int is larger than byte type. Since a smaller range type is converted into a larger range type this conversion is also known as widening conversion. Characters can never be converted to boolean type. Both are incompatible.


Explicit Conversion or Casting

There may be situations where you want to convert a value having a type of size less than the destination type size. In such cases Java will not help you. You have do it on your own explicitly. That is why this type of conversion is known as explicit conversion or casting as the programmer does this manually.

Syntax for type casting is as shown below:

(destination-type) value

An example for type casting is shown below:

int a = 10;
byte b = (int) a;

In the above example, I am forcing an integer value to be converted into a byte type. For type casting to be carried out both the source and destination types must be compatible with each other. For example, you can’t convert an integer to boolean even if you force it.

In the above example, size of source type int is 32 bits and size of destination type byte is 8 bits. Since we are converting a source type having larger size into a destination type having less size, such conversion is known as narrowing conversion.

A type cast can have unexpected behavior. For example, if a double is converted into an int, the fraction component will be lost.


Type promotion in expressions

In addition to assignment statements, there is another place where type conversion can occur. It is in expressions. An expression is a collection of variables, values, operators and method calls which evaluate to a single value.

Type promotion rules of Java for expressions are listed below:

  • All char, short and byte values are automatically promoted to int type.
  • If at least one operand in an expression is a long type, then the entire expression will be promoted to long.
  • If at least one operand in an expression is a float type, then the entire expression will be promoted to float.
  • If at least one operand in an expression is a double type, then the entire expression will be promoted to double.

To understand the above type promotion rules let’s consider the following example of expression evaluation:

Output of the above program is: Result = 8274.22

In the above program the expression is (f * b) + (i / c) – (d * s). In the first sub expression (f * b), as one operand is float, the result of the expression will be a float. In the second sub expression (i / c), char type will be promoted to int and the result of the expression will be an int. In the third sub expression (d * s), as one operand is double, the result of the expression is a double.

So the results of the sub expressions are float, int and double. Since one of them is a double, the result of the entire expression is promoted to a double.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.


You can follow any responses to this entry through the RSS 2.0 feed.

why the result of the sub expression of float,int,double is double?difference between float n double?

    Precision of double is greater than float. If you take float and double, float gets converted to double.

Leave a Reply

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

Scroll Up
Thank you for visiting! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: