1.5 Casting and Ranges of Variables
Enduring Understanding
The way variables and operators are sequenced and combined in an expression determines the computed result.
Learning Objective
Evaluate arithmetic expressions that use casting.
Essential Knowledge
The casting operators (int) and (double) can be used to create a temporary value converted to a different data type.
Casting a double value to an int causes the digits to the right of the decimal point to be truncated.
Some programming code causes int values to be automatically cast (widened) to double values.
Values of type double can be rounded to the nearest integer by (int)(x + 0.5) or (int)(x – 0.5) for negative numbers.
Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_ VALUE inclusive.
If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. This could result in an incorrect value within the allowed range.
Last updated
Was this helpful?