Sunday, April 20, 2014

Basic Java Language Fundamentals - Type Conversion

As we know Java is a strongly typed language, each data value is associated with a particular type. But sometimes it may require to convert a data value of one
type to another type, but we must be careful that we don’t lose important information in the process
.

Conversion between one primitive type to another primitive type is termed as widening and narrowing conversion.

Widening conversion: They are called widening conversions because they go from one data type to another type that uses an equal or greater amount of space to store the value. It is something like you want to put a smaller box in larger box

Example: converting from a byte to a short is called widening conversion because a byte is stored in 8 bits and a short is stored in 16 bits. There is no loss of information.
All widening conversions that go from an integer type to another integer type, or from a floating point type to another floating point type, preserve the numeric value exactly.


When converting from an int or a long to a float, or from long to double , then some of least significant digits may lost.In this case,the resulting floating point value will be a rounded version of the integer value,following the rounding techniques defined in the IEEE 754 floating point standard.

Narrowing conversion: They often go from one type to a type that uses less space to store a value, and therefore some of the information may be lost. Narrowing conversions can lose both numeric magnitude and precision. correlate this with example like, trying to put larger box in smaller box and to make that fit into you need to cut it from sides means there will be loss of something.

when we convert a byte(8 bits) or short(16 bits) to a char(16 bits), then also it is considered as narrow conversioning because the sign bit is incorporated into the new character value.

Since a character value is unsigned, a negative integer will be converted into a character that has no particular relationship to the numeric value of the original integer.

A boolean value cannot be converted to any other primitive type and vice versa so boolean type in java does not play any role in type conversion.

 Type conversion come in light during:
  • Assignment conversion
  • Arithmetic conversion
  • casting
Assignment conversion occurs when a value of one type is assigned to a variable of another type during which the value is converted to the new type. 

For example, 
float money;
int pound = 50;
Then we write money = pound ( widening ,so automatic conversion). Here it will be money = 50.0

But if we try to write as below:
pound=money (compiler will issue an error due to narrowing conversion, to complete the conversion we have to use explicit conversion(means we have to tell compiler that yes i want this conversion).

Arithmetic promotion occurs automatically when certain arithmetic operators need to modify their operands in order to perform the operation.

For example,
float sum;
int count;
result = sum/count
Here value of count is promoted to a floating point value automatically, before the division takes place, producing a floating point result.

Casting: A cast is a Java operator that is specified by a type name in parenthesis. It is placed in front of the value to be converted.

For example, to convert money to an integer value, we could put a cast in front of it: pound = (int) money;
The cast returns the value in money, truncating any fractional part. If money contained the value 87.69, then after the assignment, pond would contain the value 84.

 
Casts are helpful in many situations where we need to treat a value temporarily as another type. For example, if we want to divide the integer value total by the integer value count and get a floating point result, we could do it as follows:
result = (float) total / count;

First, the cast operator convert a floating point version of the value in total. Then, count is treated as a floating point value via arithmetic promotion. 
Now the division operator will perform floating point division and produce the intended result.
If the cast had not been included, the operation would have performed integer division and truncated the answer before assigning it to result. 

Also note that because the cast operator has a higher precedence than the division operator, the cast operates on the value of total, not on the result of the division.

No comments:

Post a Comment