Introduction to Type Conversions through Casting
Overview
In this lesson, students will learn how to manipulate data types through casting.
Learning Objectives
- 1.1.C.3 Use casting in expressions when necessary to obtain the correct type.
Skills
- S1.B Design algorithms for a program.
- S1.D Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
- S2.C Analyze an algorithm and program code for correctness.
Student Outcomes
Students will be able to:
- use casting to temporarily change the type of a value
Duration: 1 class period
Resources
Background
In the Arithmetic Expressions and Assignment lesson plan, we learned that there are two different types of division.
This means that division functions in two different ways:
- If both operands are of type
int
, the result truncates (removes) any remainder. This is considered integer division. For example:
7 / 2
results in 3
, the decimal portion is not included.
4 / 2
results in 2
, there is no remainder in this case.
- If one or both operands are of type
double
, the result includes the remainder. For example:
7.0 / 2
results in 3.5
. Similarly, 7 / 2.0
and 7.0 / 2.0
will also result in 3.5
.
4.0 / 2
results in 2.0
. Similarly, 4 / 2.0
and 4.0 / 2.0
will also result in 2.0
.
There are often times when you have two int
values or variables and want to compute division and have the result include the remainder. There are also times when one of the values is a double
and we need the whole part of the division.
For example, maybe you are computing a student average. You have two int
values: the sum of all the grades and the number of grades that you have. When we compute the average of the the two int
values, it will truncate the result, essentially always rounding down. Consider the following:
int sumGrades = 90 + 85 + 95 + 95;
int gradeCount = 4;
int average = sumGrades / gradeCount; //average is assigned the value 91
Instead, you might want to have the real-number average. To do this, you can cast one or both of the operands to be a double
. Casting Conversions converts the type of an expression to the type specified. It is done by placing the desired type in parenthesis prior to the expression. Consider the following:
int sumGrades = 90 + 85 + 95 + 95;
int gradeCount = 4;
double average = (double)sumGrades / gradeCount; //converts sumGrades to type double; average is assigned the value 91.25
average = sumGrades / (double)gradeCount; //converts gradeCount to type double; average is assigned the value 91.25
average = (double)sumGrades / (double)gradeCount; //converts both sumGrades and gradeCount to type double; average is assigned 91.25
When we type cast from an int
to a double
, this is considered a widening conversion since the int
value fits into the double
type without any loss of data.
When we type cast from a double
to an int
, this is considered a narrowing conversion since the double
value might need to be truncated to fit into the int
type thereby losing some data. For example:
double average = 89.75;
int truncatedAverage = (int)average; //truncatedAverage is assigned the value 89
Activity
Activity 1: Mini-Lab
Have students use the Java Playground to write code to calculate a test grade when 60% of the score comes from a multiple choice section with 25 questions and 40% comes from a free response question with 3 parts. Each question on the multiple choice section is worth the same amount and each part of the free response exam is worth the same amount. Provide them with the following starter code:
int mCQnumCorrect = 20;
double multipleChoicePart;
int fRQCorrect = 2;
double frqPart;
int score;
//add code to compute multipleChoicePart, frqPart, and score
//use proper type casting to get the correct answer of 74