What’s going on inside a computer?
- CPU can do very simple things with a small amount of data that has been pulled into the CPU.
- CPU pulls that data from memory and when it’s done often puts new values back into memory.
- In high-level languages like Java we don’t have to keep track of where values live in memory and we definitely don’t have to keep track of moving data in and out of the CPU; the lanuage takes care of all of that for us.
x = x + 1
turns into, figure out where x
lives in memory; load the value from memory into the CPU; execute the CPUs addition function to change the value in the CPU by 1; then store the value from the CPU back to the original location in memory.
Variables
- Variables in programming are different than variables in math in that they can change value.
x = x + 1
is sensible in programming but nonesense in math.
- Each variable has associated memory that is used to hold its value.
- Declaring a variable is how we say, this is a name that we are going to use and this is the type of value it will hold. Each variable is declared once.
- There are two parts of creating a variable: declaring the variable and initializing it.
- Initializing a variable is how we assign a variable its initial value. This can happen at the point we declare the variable or later.
- Member variables in classes will automatically be initialized to a default value if we don’t explicitly initialize them.
- The memory associated with a variable of a primitive type holds the actual representation of the primitive value while the memory associated with a variable of a reference type holds a reference to the actual data which exists somewhere else in the computer’s memory.
- Some variables exist only to provide names for special values and cannot be changed. They are called “constant variables” or just “constants”. You’ll learn about
Math.PI
which is the best approximation to 𝜋 and Integer.MAX_VALUE
and Integer.MIN_VALUE
which give the largest and smallest int
values.
Declaring variables
- A minimal variable declaration contains a type and a name.
- The type of a variable determines what kind of values we can assign to the variable and what kind of value it produces when used as an expression.
- The name of a variable allows us to refer to it in assignment expression and as a value.
- When a variable is declared
final
, its value cannot be changed once it is initialized.
- There are two kinds of variables in Java: local and member. Member variables, are further divided into instance and static variables.
- Local variables are declared in the body of methods and constructors. These variables may only be used within the method or constructor where they are declared. They are not marked as either
public
or private
because they are not accessible at all outside the method or constructor.
- Parameters declared in a method or constructor are also local variables.
- When there is a local variable with the same name as a member variable, the name will refer to the local variable instead of the member variable.
- Member variable are declared at the top-level of a class and can be marked as
public
or private
. Usually variables will be marked private
though sometimes classes define important constants as public static final
variables, for instance Math.PI
.
- By strong convention all member variables are declared at the beginning of the class.
Variables holding reference types
- Because the value of a reference type is the reference, not the actual memory representing the referenced value, multiple variables can hold references to the same object, i.e. the value stored in each variable is the same reference. (This is, in fact, exactly what it means for two variables to be
==
.)
- When multiple variables hold the same reference, changes made to the referenced object via one variable will be be visible via the other variable.
Assignment operators
- The thing that we can do with variables is \i{assign} them values. Unless a variable is declared with a
final
modifier it can be assigned a new value at any time. Assigning a new value to a variable has no effect on the value that was previously assigned to that variable; it just means the variable is now a name for the new value instead of the old value.
- The assignment operator
=
allows a program to initialize or change the value stored in a variable or an array element. The value of the expression on the right of the =
is stored in the variable or array element referenced on the left.
- The only assignable places in Java are variables (local or member) and array access expressions, e.g.
x
, obj.someField
, and nums[0]
. We’ll talk about arrays and member variables later.
- Assignments in Java are technically also expressions that produce a value, namely the value that was assigned, but normally we care more about their side effect, namely changing the value the variable or array element being assigned to. (In general using the value of an assignment expression is a bad idea.)
- Compound assignment operators (
+=
, −=
, *=
, /=
, %=
) can be used to assign a new value to a variable or array element computed from the old value. For instance x += 2
is equivalent to x = x + 2
. The value of a compound assignment expression is the new value of the variable or array element.
- The increment operator (
++
) and decrement operator (−−
) are used to add 1 or subtract 1 from the value of a variable or an array element. For instance x++
equivalent to x += 1
or x = x + 1
. The value of a ++
or --
is the value of the variable or array element \i{before} it was incremented or decremented.\note{The AP curriculum does not cover it but these operators can also be used in prefix form, like ++x
in which case the value of the expression is the new value of the variable rather than the old value.}
- Note that
=
(one equals sign) is the assignment operator and ==
(two equals signs) is the equality comparison operator. If you mix them up, you’ll get either compiler errors or incorrect and confusing behavior from your program. Remember: = != ==
.
Casting
- The casting operators
(int)
and (double)
produce numeric values of the specified type.
- Casting a
double
to an int
produces an int
with any fractional part (the digits to the right of the decimal point) truncated.\note{The AP curriculum does not touch on this but casting a double
value outside the range that can be represented by an int
results in either Integer.MAX_VALUE
or Integer.MIN_VALUE
.}
- Casting an
int
to double
produces a double with the same mathematical value since all int
values are exactly representable as a double
.
Variable scope
- Every variable has a \i{scope} which is the textual part of the program in which the name is meaningful. This will become more important in future units when we talk about writing methods and control constructs.
- In general we want to minimize the scope of variables, i.e. declare them as near to where they need to be used, so we don’t have to look at too much code to understand how they are used.
- Variables also have “extent” which is how long they exist in time. For intstance the parameters of a method are scoped to the body of the method (can only be referred to within the text of the method) and have exent of the duration of the method call—once the method returns the variables no longer exist. (Though when the method is called again, a new set of variables will come into existance and last for as long as that method call takes.)