Using var As A Data Type in Programs
Overview
In this lesson, students will begin to develop an understanding of when it is appropriate to use var and when it is not appropriate.
For AP CSA teachers, the use of var is outside the scope of the AP CSA course and exam.
Learning Objectives
- 1.4.A.3 Use
varto create variables of inferred types.
Skills
- S1.C Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
Student Outcomes
Students will be able to:
- determine when
varcan be used instead of a data type.
Duration: 1 class period
Resources
Warm-up / Motivate
The use of var for a data type works because the compiler can intuitively determine what type of data is being stored based on what is assigned to the variable. It is a short-cut that can be used. Students should have a sound understanding of data types before beginning to use var instead of explicitly defining the data type.
Learn
Either as a group or on their own, have students review the tutorial: Learn - Local Variable (var)
Practice
Have students complete practice exercises: Practice - Exploring var with the Java Playground
Apply
Incorporate var into a prior program.
- Have students revisit a prior program and include
varin all places that are unambiguous. - Identify places in the program where the use of
varcannot be used. - Identify places in the program where the use of
varcan be used, but shouldn't be used.
Wrap-up / Extension
Using var on AP CSA Free Response Questions
How does this look on the exam?
Now that we have taken a chance to understand var outside of the AP CSA exam. Let's look at an example of how students might use this on the AP Computer Science A (AP CSA) exam.
The following are examples of how students could use var on the AP CSA Free Response Exam. You can find all of the questions here
Free Response Question 1 - Bird Feeder
This question involved a simulation of a bird feeder and requires students to do the following:
- generate random values
- use if statement
- use loops
Part A - Example Canonical Solution
There are three variable declarations in the sample solution above:
bearPresent– is assigned to theBoolean false. The type is unambiguous with the assignment.bear– is assigned to adoublefromMath.random(). You could usevar, but it can be ambiguous to use with a method return.birdConsumed– even though there is a method call, the typecast to anintmakes the type clear that the value being assigned is anint.
A sample solution using var is as follows:
Part B - Example Canonical Solution
There are two variables declared in the sample solution above:
count- Since the variable is being assigned to1, it is unambiguous and is a good place to usevar.days- The for loop counterdayscan be declared usingvar.
This sample solution using var is as follows:
Free Response Question 2 - Scoreboard
In this free response question, students are asked to write a class for a scoreboard with two teams. Students will need to:
- declare instance variables
- write constructors
- write methods
- use if statements
The following is a sample solution:
In this solution, we have many data types where students might be tempted to use var. However, you cannot use var for the following:
- instance variables
- constructor or method parameters
- method return types
If a student were to use var in these places, they will likely lose points.
There is only one place in the solution above where a student could use var if they wanted to do so. In the getScore method the String variable, retVal, is declared and assigned the concatenation of team scores and a string hyphen. Since the concatenation of strings with int values creates a String, it is unambiguous what type we are using and var can be used appropriately.
A sample solution using var is as follows:
NOTE: In this solution we have used the ternary conditional operator. This operator is not a part of the AP Java Subset. You do not need to teach this operator.
Learn more on the Ternary Conditional Operator.
Free Response Question 3 - WordList
In this free response students will need to:
- loop over a list
- access adjacent elements of a list
- use string methods
- add values to a list
- create a new list
Part A - Sample Canonical Solution
In part A of this free response question, students are asked to determine if the previous word is a substring of the next word.
In this solution, there are three variables being declared:
index- the for loop counterindexcan be declared withvarnextWordandcurrentWord- students could usevarto declare these variables, but this would be considered less readable, since the value these are being assigned comes from a method call. In this case, we are using common method calls and will likely know thatwordliststoresStringvalues, but it is still considered less than desirable to use it in these two cases.
A sample solution using var is as follows:
Part B - Sample Canonical Solution
In part B, students need to make a list of all word that have the target word at the beginning, but with the beginning of the word is removed.
In this solution, var could be used in two places:
retList- a newArrayListthat is able to store string valuesword- used in the enhanced-for loop to store the values inwordList, which are strings
A sample solution using var is as follows:
Free Response Question 4 - GridPath
In this free response students will need to:
- access elements in a 2D array
- use a provided class
- use conditionals and loops
Part A - Sample Canonical Solution
In this part, students need to return the smaller of two values, either below or to the right if they exist.
In this solution, we have declared four variables:
numRowsandnumCols- The value assigned tonumRowsandnumColsis from the constant length. Since this is really common, it is most likely readable to usevarin these cases. However, if there is any doubt about the readability, you will want to use the type and be clear.belowandright- The values assigned tobelowandrightare what is being stored ingridand is not ambiguous.
A sample solution with var is as follows:
NOTE: In this solution we have used the ternary conditional operator. This operator is not a part of the AP Java Subset. You do not need to teach this operator.
Learn more on the Ternary Conditional Operator
Part B - Sample Canonical Solution
In this solution, students are summing the values along a path using the return value from getNextLocation.
In this solution, we have two variables that are being declared and assigned values:
total- which is unambiguous being assigned to0.next- assigned a returned type from a method call. This method is a local method and would be easy to find, however, it is still considered less readable to usevarin this case.
A sample solution with var is as follows:
Final Thoughts on Using var with AP CSA
Use of var and the AP CSA Free Response Questions
- Using
varis OUTSIDE the AP CSA Java Subset - Students can write correct solutions using
var - Using
varcan be used in any FRQ. Students need to be careful using it with FRQ 2, since they cannot use it with instance variables, parameters, and return types.
Scoring and Teaching
- Student solutions that use correct Java syntax can earn full credit for their solution
- Questions are written with an in AP Java subset solution in mind
- Students should be encouraged to write solutions within the AP Java Subset
- Students could lose points if they use
varand do not assign the variable to a value that is not unambiguous.