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
var
to 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
var
can 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
var
in all places that are unambiguous.
- Identify places in the program where the use of
var
cannot be used.
- Identify places in the program where the use of
var
can 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
public void simulateOneDay (int numBirds) {
boolean bearPresent = false;
double bear = Math.random() * 100;
if (bear <= 5) {
bearPresent = !bearPresent;
}
int birdConsumed = ((int) (Math.random() * 41) + 10) * numBirds;
if (bearPresent)
{
currentFood = 0;
}
else{
if (birdConsumed >= currentFood) {
currentFood = 0;
else {
currentFood = currentFood - birdConsumed;
}
}
There are three variable declarations in the sample solution above:
bearPresent
– is assigned to the Boolean false
. The type is unambiguous with the assignment.
bear
– is assigned to a double
from Math.random()
. You could use var
, but it can be ambiguous to use with a method return.
birdConsumed
– even though there is a method call, the typecast to an int
makes the type clear that the value being assigned is an int
.
A sample solution using var
is as follows:
public void simulateOneDay (int numBirds) {
var bearPresent = false;
double bear = Math.random() * 100;
if (bear <= 5) {
bearPresent = !bearPresent;
}
var birdConsumed = ((int) (Math.random() * 41) + 10) * numBirds;
if (bearPresent)
{
currentFood = 0;
}
else{
if (birdConsumed >= currentFood) {
currentFood = 0;
else {
currentFood = currentFood - birdConsumed;
}
}
Part B - Example Canonical Solution
public int simulateManyDays(int numBirds, int numDays) {
int count = 1;
for(int days = 1; days <= numDays; days++) {
if(currentFood == 0) {
return days;
}
simulateOneDay(numBirds);
}
return numDays;
}
There are two variables declared in the sample solution above:
count
- Since the variable is being assigned to 1
, it is unambiguous and is a good place to use var
.
days
- The for loop counter days
can be declared using var
.
This sample solution using var is as follows:
public int simulateManyDays(int numBirds, int numDays) {
var count = 1;
for(var days = 1; days <= numDays; days++) {
if(currentFood == 0) {
return days;
}
simulateOneDay(numBirds);
}
return numDays;
}
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:
public class Scoreboard {
private String team1;
private String team2;
private String activeTeam;
private int team1Score;
private int team2Score;
public Scoreboard(String t1, String t2) {
team1 = t1;
team2 = t2;
activeTeam = team1;
team1Score = 0;
team2Score = 0;
}
public void recordPlay(int points) {
if (points == 0) {
activeTeam = (team1.equals(activeTeam)) ? team2 : team1;
} else {
if (team1.equals(activeTeam)) {
team1Score += points;
} else {
team2Score += points;
}
}
}
public String getScore() {
String retVal = team1Score + " – " + team2Score + " – " ;
retVal += (team1.equals(activeTeam)) ? team1 : team2;
return retVal;
}
}
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:
public class Scoreboard {
private String team1;
private String team2;
private String activeTeam;
private int team1Score;
private int team2Score;
public Scoreboard(String t1, String t2) {
team1 = t1;
team2 = t2;
activeTeam = team1;
team1Score = 0;
team2Score = 0;
}
public void recordPlay(int points) {
if (points == 0) {
activeTeam = (team1.equals(activeTeam)) ? team2 : team1;
} else {
if (team1.equals(activeTeam)) {
team1Score += points;
} else {
team2Score += points;
}
}
}
public String getScore() {
var retVal = team1Score + " – " + team2Score + " – " ;
retVal += (team1.equals(activeTeam)) ? team1 : team2;
return retVal;
}
}
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.
public boolean isWordChain() {
for (int index = 0; index < wordlist.size() – 1; index++) {
String nextWord = wordlist.get(index + 1);
String currentWord = wordlist.get(index);
if(nextWord.indexOf(previous) == -1) {
return false;
}
}
return true;
}
In this solution, there are three variables being declared:
index
- the for loop counter index
can be declared with var
nextWord
and currentWord
- students could use var
to 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 that wordlist
stores String
values, but it is still considered less than desirable to use it in these two cases.
A sample solution using var
is as follows:
public boolean isWordChain() {
for (var index = 0; index < wordlist.size() – 1; index++) {
String nextWord = wordlist.get(index + 1);
String currentWord = wordlist.get(index);
if(nextWord.indexOf(previous) == -1) {
return false;
}
}
return true;
}
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.
public ArrayList<String> createList(String target) {
ArrayList<String> retList = new ArrayList<String> ();
for (String word : wordList) {
if (word.indexOf(target) == 0) {
retList.add(word.substring(target.length()));
}
}
return retList;
}
In this solution, var
could be used in two places:
retList
- a new ArrayList
that is able to store string values
word
- used in the enhanced-for loop to store the values in wordList
, which are strings
A sample solution using var
is as follows:
public ArrayList<String> createList(String target) {
var retList = new ArrayList<String> ();
for (var word : wordList) {
if (word.indexOf(target) == 0) {
retList.add(word.substring(target.length()));
}
}
return retList;
}
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.
public Location getNextLoc(int row, int col) {
int numRows = grid.length;
int numCols = grid[0].length;
if (row + 1 == numRows)
return new Location(row, col + 1);
if (col + 1 == numCols)
return new Location(row + 1, col);
int below = grid[row + 1][col];
int right = grid[row][col + 1];
return (below < right) ? new Location(row + 1, col) : new Location(row, col + 1);
}
In this solution, we have declared four variables:
numRows
and numCols
- The value assigned to numRows
and numCols
is from the constant length. Since this is really common, it is most likely readable to use var
in these cases. However, if there is any doubt about the readability, you will want to use the type and be clear.
below
and right
- The values assigned to below
and right
are what is being stored in grid
and is not ambiguous.
A sample solution with var
is as follows:
public Location getNextLoc(int row, int col) {
var numRows = grid.length;
var numCols = grid[0].length;
if (row + 1 == numRows)
return new Location(row, col + 1);
if (col + 1 == numCols)
return new Location(row + 1, col);
var below = grid[row + 1][col];
var right = grid[row][col + 1];
return (below < right) ? new Location(row + 1, col) : new Location(row, col + 1);
}
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
.
public int sumPath(int row, int col) {
int total = 0;
while (row < grid.length – 1 || col < grid[0].length – 1) {
total += grid[row][col];
Location next = getNextLocation(row, col);
row = next.getRow();
col = next.getCol();
}
return total;
}
In this solution, we have two variables that are being declared and assigned values:
total
- which is unambiguous being assigned to 0
.
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 use var
in this case.
A sample solution with var
is as follows:
public int sumPath(int row, int col) {
var total = 0;
while (row < grid.length – 1 || col < grid[0].length – 1) {
total += grid[row][col];
Location next = getNextLocation(row, col);
row = next.getRow();
col = next.getCol();
}
return total;
}
Final Thoughts on Using var with AP CSA
Use of var and the AP CSA Free Response Questions
- Using
var
is OUTSIDE the AP CSA Java Subset
- Students can write correct solutions using
var
- Using
var
can 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
var
and do not assign the variable to a value that is not unambiguous.