Turn Desktop View Off
by Dinesh Thakur

In addition to the implicit type conversion, Java also provides the facility of explicit type conversion within an expression. In implicit type conversion, a value of lower data type is converted to a value of higher data type. This results in no loss of information. However, if you try to convert a value of higher data type to that of lower data type then java compiler generates an error message "possible loss of precision". For example:

float i = 12.50;

But if you want to force this conversion which may result in loss of information then this can be accomplished using explicit type casting.

Using explicit type casting, we can override Java's default type conversions by explicitly specifying our own temporary data type according to the requirements. When we type cast a value, its data type is changed temporarily from its declared data type to the new data type. To explicit type cast, give the target type in parentheses followed by the variable's name or the value to be cast.

(target_data type) operand;

For example:

float height = (float)5.5;

int d = (int ) (a/b); // Here a and b are of double type

In the first example, integer literal 5.5 is converted to float type and result is stored in variable height. The cast is necessary because there is no automatic conversion from double to float. In the second example, the outcome of the expression is converted to into Parentheses surrounding a/b is necessary, otherwise the cast to int will only apply to a and not to the outcome of the result.

Explicit type conversion is also known as narrowing a type. The explicit type casting is important in situations where the fractional part of a number is to be preserved. E.g. If you want to calculate the percentage marks of student appearing in examination in five different subjects of 100 marks each and suppose that total marks obtained is stored in a variable tot_marks_obt of type int, then on executing the statement.

per = tot_marks_obt/5;

gives you the percentage as an integer value and fractional part will be ignored. Thus in

this case, to retain fractional part you need to cast variable tot _marks _ obt to type double before dividing it by 5 as shown in the following statement.

per = (double)tot_marks_obt/5;

In the above statement, the type of variable tot_marks_obt is converted explicitly into double data type and stored in temporary variable of type double which is then divided by 5.0 (implicitly conversion from int to double) and this percentage calculated in fraction form stored in variable per. Here, the conversion does not change the original data type of operand and type is temporarily changed within the expression.

To illustrate the concept of explicit typecasting, let us consider a program to convert temperature given in Fahrenheit to one in Celsius.

/* Program to Convert temperature given in Fahrenheit to celsius using Explicit type conversion */

class ExplicitTypeConversion

{

         public static void main(String args[])

    {

          double c;

          double f=96;

          c = (double) 5/9 * (f-32);

           System.out.println("Temperature in Celcius is : " +c);

     }

}