Skip to main content

Type conversion in Java

Type conversion in Java

When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.

Ø  Implicitly  Type Conversion
Implicit ( hidden ) type conversion is only possible when there is no possibility of data loss in the conversion , i.e. When you convert type to a smaller range to type with greater ( for example from int to long). To make implicit conversion is not need to use any operator , so called hidden. The conversion is done automatically by the compiler when mapping value of small-scale variable in a larger scope, or when expression types has a different scope. Then convert it to type with a greater range .

Implicit Type Conversion - Example
Here is an example of implicit (implicit) type conversion :

int VarInt = 5;
System.out.println(VarInt); // 5
long Varlong = VarInt;
System.out.println(Varlong); // 5
System.out.println(Varlong + VarInt); // 10

Possible Transformations Implied
These are possible implicit conversion of primitive types in Java:

1. Byte to short,int,long,float,or double
2. Short to int,long,float,or double
3. Char to int,long,float,or double
4. Int to long,float,or double
5. Long to float or double
6. Float to double

Conversion of types from small-scale to a larger no loss of data . The numerical value remains the same after conversion. As with any rule there are few exceptions. when convert type int to type float ( 32-bit values) , the difference is int that uses all your bits for a single integer including as part of the float bits used for the presentation of Float . Hence, it is possible in the conversion of int to float to a loss of accuracy due to rounding . The same concerns the conversion of 64-bit long to double.

Explicitly Type Conversion
Explicit type conversion is needed when it is probable loss of data. When you convert a floating-point type to an integer type, there is always loss of data coming from the float and must use explicit conversion (double to long). To make such conversion is necessary to explicitly use operator for data conversion (cast operator): (type). possibly There may be data loss also when converting from type larger range to type with less (double to float or long to int).

Explicit Type Conversion - Example
The following example illustrates the use of an explicit conversion types and data loss:

double VarDouble = 5.1d;
System.out.println(VarDouble); // 5.1
long Varlong = (long)VarDouble;
System.out.println(Varlong); // 5
VarDouble = 5e9d; // 5 * 10^9
System.out.println(VarDouble); // 5.0E9
int VarInt = (int) VarDouble;
System.out.println(VarInt); // 2147483647
System.out.println(Integer.MAX_VALUE); // 2147483647


The first line of the example you assign a value to the variable 5.1 VarDouble. Once you convert it (explicitly) by the operator (long) long to type and print the console variable Varlong, we see that the variable is the value lost after float (for long to integer). Then on the seventh row Assign variable VarDouble 5 billion. finally convert VarDouble to int by the operator (int) and print variable VarInt. Then same result as when print Integer MAX_VALUE, this is because VarDouble incorporates more value of the range of int.

Comments