π1.6: Casting
Table of Contents
π This page is a condensed version of CSAwesome Topic 1.6
Casting & Ranges of Values
In Java, type casting is used to convert values from one type to another. By casting we donβt mean something to do with fishing, but it is a similar idea to casting a bronze, without needing to heat anything to 913 degrees Celsius. But like molten bronze is reshaped by melting it and pouring it into a mold, our data is reshaped via a cast operator. In Java when you cast you are changing the βshapeβ (or type) of the value.
The cast operator, which looks like (int)
and (double)
placed before any expression (a literal, a number, a variable, or more complex expression in parentheses) produces a value of the given type by converting the value of the original expression to the new type.
For example,
(double) 1 / 3
will evaluate to adouble
value instead of anint
.
- Run this code to find how Java handles division and what casting can do to the results. Notice what happens when you divide an
int
by anint
or anint
by adouble
or anint
cast to adouble
divided by anint
.
System.out.println(3 / 4); // int divided by int
System.out.println(3.0 / 4); // double divided by int
System.out.println(3 / 4.0); // int divided by double
System.out.println((double) 3 / 4); // int cast to double, divided by int
System.out.println((int) 3.0 / 4); // double cast to int, divided by int
- What happens when you divide an int by an int or with a double operand or with the type cast (double) or (int) on one of the operands? Add another line that divides 5 by 2 using a
(double)
cast. What is the result?
Conversions in Calculations
When Java divides two int
\ s, it produces an int
result by truncating the actual mathematical result, removing anything after the decimal point. Thus 9 / 10
evaluates to 0
, not 0.9
. It also does not evaluate to 1
; truncating is not the same as rounding!
But in any expression involving a double
, the double
is βcontagiousβ and will cause the value of that expression to also be a double
. Thus the expression 9.0 / 10
is evaluated as if it had been written 9.0 / 10.0
and produces the double
value 0.9
.
Casting an
int
todouble
, as shown in the code above, produces adouble
value which will then causes any expression it is part of to produce adouble
. This is especially useful when you haveint
variables that you want to do non-integer division with.
A conversion from int
to double
is called a widening conversion because a double
can represent any int
value but not vice versa; thus a double
is considered a wider data type than an int
.
Rounding by Casting
Values of type double
in the range that can be represented by an int
can be rounded to the nearest int
by adding or subtracting 0.5 and then casting the result with (int)
:
double number; // positive value from somewhere
double negNumber; // negative value from somewhere
int nearestInt = (int)(number + 0.5);
int nearestNegInt = (int)(negNumber β 0.5);
For example, if you divide
7.0 / 4.0
you get1.75
. If you cast that to anint
, it will be truncated to1
.However if we want to round a
double
rather than truncating it, adding0.5
will produce a number that is above the next integer value if the decimal part is greater than0.5
, as it is here. Then casting that value to anint
will truncate down.
Precision
What happens to repeating decimal numbers like 3.333333β¦? Java limits the number of digits you can save for any double
number to about 14-15 digits. You should be aware that the accuracy of any calculation on a computer is limited by the fact that computers can only hold a limited number of digits.
For example, int values are stored in 4 bytes of memory. There is an Integer.MAX_VALUE
defined as 2147483647 and an Integer.MIN_VALUE
defined as -2147483648. If you try to store any number larger or smaller than these numbers in an int variable, it will result in an integer overflow where an incorrect value could be stored. Try it below.
βοΈ Summary
Summary
-
Type casting is used to convert values from one type to another.
-
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 anint
causes the digits to the right of the decimal point to be truncated (cut off and thrown away). -
In expressions involving
double
s, thedouble
values are contagious, causingint
s in the expression to be automatically converted to the equivalentdouble
value so the result of the expression can be computed as adouble
. -
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 fromInteger.MIN_VALUE
toInteger.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.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.