declare, initialize and assign a value to a variable
identify the most appropriate data type category for a particular specification
categorize a data type as either primitive or reference
declare a final variable
WHAT IS A VARIABLE?
A variable is a name associated with a memory location in the computer.
Computer memory can store a value that can change or vary.
To create a variable in Java, you must first identify the data type of the variable.
There are three data types that you need to be familiar with for the exam:
1) If you wanted to store a whole number representing the number of apples you picked in the orchard, use an int!
int apples = 25;
2) If you wanted to store a decimal number representing the cost of an energy drink, use a double!
double cost = 3.55;
3) If you want to store a true or false value to represent if it is snowing, use a boolean!
boolean isSnowing = true;
When you play a game, it will often have a score. Typically, your score will start at 0 and increase based on events that take place in the game. The score of a player is a great value to store in a variable named "score".
DATA TYPES
There are two types of variables in Java: primitive types that hold primitive types and object variables that hold a reference to an object of a class.
I think of a reference as a way to find the object (like a UPS tracking number helps you to find your package).
The three primitive types on the exam are:
1) int - which store integers (whole numbers like 3, -76, 20393)
2) double - which store floating point numbers (decimal numbers like 6.3, -0.9, and 6134.12344)
3) boolean - which store boolean values (either true or false)
String is one of the object types on the exam and is the name of a class in Java.
A String object has a sequence of characters enclosed in a pair of double quotes - like "Hello".
You can tell that String is an object type variable because the S is capitalized.
We will learn more about String objects later.
A type is a set of values (a domain) and a set of operations on them.
For example, you can do addition with ints and doubles but you can't do addition with booleans and Strings.
DECLARING VARIABLES IN JAVA
There are two steps to using variables:
1) Declare the variable by telling Java its data type AND name.
The data type is a keyword. Use int for integers, double for floating point numbers (decimal precision numbers), and boolean for Boolean values (true or false).
You get to make up the name for the variable!
2) Initialize the variable (technically optional)
For example, if you wanted to declare a variable called score, you would type:
int score;
Later, you could initialize (give an initial value) the variable by typing:
score = 0;
Generally, I tend to declare and initialize variables using a single line of code:
int score = 0;
The equal sign = doesn't mean the same thing that it does in a mathematical equation where it implies that the two sides are equal.
Here the equal sign means set the value in the memory location associated with the variable name on the left to a copy of the value on the right.
The variable always has to be on the left side of the = and the value or expression on the right.
I typically refer to the = operator as the assignment operator and try very hard to avoid saying "equals" when I use it.
Try to get into the habit of saying "score gets assigned the value zero" instead of "score equals zero".
VARIABLES IN MEMORY
When you create a primitive variable, Java will set aside enough bits in memory for that primitive type and associate that memory location with the name you used.
Computer store all values using bits (binary digits).
A bit can represent two values and we usually say that the value of a bit is either 0 or 1.
When you declare a variable, you have to tell Java the type of variable because Java needs to know how many bits to use and how to represent the value.
The three different primitive types (int, double, and boolean) all require different numbers of bits.
An integer gets 32 bits of space, a double gets 64 bits of space, and a boolean could be represented with just one bit (0 for false and 1 for true).
CONCATENATION
When you want to print out variables, you can use the string concatenation operator + to combine them with another string inside the print command.
Don't put the variable name between quotes "" because that will print out the variable name and not the value stored in the variable.
Again, remember that when concatenating (combining) you are responsible for adding spaces to obtain the proper output.
Another good habit to get into referring to the + operator as the concatenation operator.
I try very hard to avoid saying "plus" when I use it and say "concatenate" instead.
The tricky thing is that when you use the + operator with integers they will be added together as you would expect algebraic addition to work in math.
So, the + operator can be used for concatenation AND addition.
Java knows when to perform concatenation or addition as long as you do...
FINAL KEYWORD
The keyword final can be used in front of a variable declaration to make it a constant that cannot be changed.
The variable names of constants are traditionally capitalized.
NAMING VARIABLES
You can name your variables almost anything, but there are some rules...
A variable name should always start with an alphabetic character (a, b, c, etc.) and can include letters, numbers, and underscores _.
Variable names MUST be all one word without spaces.
You can't use any of the keywords or other reserved words as variable names in Java (for, if, class, static, int, double, etc.).
The name of the variable should be meaningful and describe the data it holds.
A name like score helps make your code easier to read.
Actual humans will be grading the code that you write on the AP exam, so the easier you can make it to read your code, the better.
A name like x would not be a good name because it doesn't give any clues about the kind of data it holds.
Don't name your variables crazy things like thisIsAReallyLongName, especially on the exam.
I typically use camelCase when naming my variables.
When you use camelCase, the first word in the variable name is not capitalized but each additional words is capitalized.
camelCaseLooksLikeThis
Another option would be to use underscores _ to separate each word since spaces are not allowed:
this_variable_name_has_underscores
Finally, variable names are case-sensitive AND spellingsensitive! In order to use the variable, the code must match the variable name in the declaration exactly.
SUMMARY
A variable is a name for a memory location where you can store a value that can change or vary.
A variable can be delcared and initialized like this:
int score; // variable declaration
double gpa = 3.5; // declaration and initialization
Data types can be primitive types (like int) or reference types (like String).
Three primitive data types used in this course are int (integer numbers), double (decimal numbers), and boolean (true or false).
Each variable has associated memory that is used to hold its value.
The memory associated with a variable of a primitive type holds an actual primitive value.
When a variable is declared final, its value cannot be changed once it is initialized.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.