Java is a strongly typed language, which means all variables are given types, and that the type of each variable cannot be changed.

 

Section 1 - Widening Conversions

 

For compatible primitive data types, Java will implicitly convert a literal, variable, expression, or method return from the smaller type to the larger type. This is called a widening conversion, because the new data type is wider than the original type, so no information is lost.

 

In the example below, the variable b is of primitive type byte which occupies one byte of memory. It contains the decimal value 20, which is the value 14 in hexadecimal. When the byte variable b is assigned to int variable i, Java implicitly performs a widening conversion and copies the value of the byte into the lowest byte of the memory occupied by int variable i. The upper three bytes of i contain zero. When printed, both variables display the value 20.

 

        

 

All integer and floating point primitive types are compatible. An integer type will be implicitly converted to a floating point type. When this occurs, the fractional component of the floating point number is zero.

 

int i = 20;
float f = i;                        // Implicitly performs widening conversion
System.out.println(i);                // Prints 20
System.out.println(f);                // Prints 20.0

 

 

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.