Java – Type Conversion and Casting


  • In Java, there are two kinds of data types – primitives and references.  Primitives include int, float, long, double etc. References can be of type classes, interfaces, arrays.
  • A value can change its type either implicitly or explicitly.

Implicit and Explicit changes:

There are situations in which the system itself changes the type of an expression implicitly based on some requirement.


int num1 = 4;

double num2;

double d = num1 / num2;             //num1 is also converted to double

This kind of conversion is called automatic type conversion.

Type Casting:

In some cases changes do not occur on its own, the programmer needs to specify the type explicitly. This is called casting. However, conversion and casting can be performed, only if they are compatible with the some rules defined.

Syntax for casting:

Identifier2 = (type) identifier1;


num 1= (int) num2;

Widening vs. Narrowing Conversion:

Basically, primitives are converted using either one of the two forms.

Widening Conversion:

 Changes a value to a type with a larger range. This will not result in any data loss.

Example, converting from int to long.

Narrowing Conversion:

 Changes a value to a type with a shorter range. Some data may get lost.

Example, converting from double to float.

Conversion and Casting of Primitives:

There are three situations in which conversion of primitive type takes place.

  • Assignment
  • Arithmetic Promotion
  • Method call



    Assignment Conversion:

Assignment conversion occurs when a variable of one type is assigned to another type. Only widening conversions can take place through assignment.


double x;

int  y = 50;

x = y;

Arithmetic promotion:

Arithmetic promotions occur in arithmetic expressions where the operator requires the operands to be meaningful while performing the operation.


int count =7;

double sum = 24;

double average = sum / count;                  //count is also converted to double

byte b = 5;

byte++;                                                                                //byte is converted to int, then ++ is performed

Method call:

Conversions occur during a method call, when an argument of a type is passed to a method which expects a different type.


float f = 1.234f

double d = Math.cos(f);                                //method cos() expects double and hence f is converted to double

Casting of primitives:

Casting represents mentioning of the type explicitly to make a conversion. Since widening conversions are implicit by default, casting is useful for performing narrowing conversions. Narrowing conversion has the risk of losing information.


int a = 5;

short b = (short) a;

Conversion and Casting of Reference Types:

References are of type class, interface, and array. Reference type conversion cannot have arithmetic promotion since object references cannot be arithmetic operands.

Assignment conversion:

Assignment conversion happens when a reference type is assigned to a value of different type.

The rules for object reference conversion can be stated as follows:

  • An interface type may only be converted to an interface type or to Object. If the new type is an interface, it must be a super interface of the old type.
  • A class type may be converted to a class type or to an interface type. If converting to a class type, the new type must be a super class of the old type. If converting to an interface type, the old class must implement the interface.
  • An array may be converted to the class Object, to the interface Cloneable or Serializable, or to an array. Only an array of object reference types may be converted to an array, and the old element type must be convertible to the new element type.



Method invocation conversion:

Conversion of reference type can also occur when a reference type is passed to a method which accepts argument of a different type. Conversion to a super class is permitted but conversion to a subclass type is not permitted.



Casting of reference types:

  • Casting of reference type is similar to casting of primitives.
  • Rules for casting are same for that of assignment and method invocation conversion.


    PREV     NEXT

Share this website !!!
Facebook Twitter Linkedin