references Flashcards
a computer has two forms of memory
primary - main memory, used when running a program, values stored in variables are kept in main memory
secondary memory - used to hold files for permanent storage
what does main memory consist of
long list of numbered locations called bytes - each byte is 8 bits
what is the number called that identifies a byte
address
- a data item can be stored in one or more of these bytes
- the address of the byte is used to find the data item when needed
what happens if more than one byte is needed to store something
several adjacent bytes are used
what is memory location?
an entire chunk of memory that holds the data is called memory location
what happens when a variable is in primitive type?
- every variable is implemented as a location in computer memory
- when in prim form - the value of the variable is stored in the memory location assigned to the varibale
- each prim type always require the same amount of memory to store its values
what happens when the variable is a class type?
only the memory address or reference where its object is located is stored in the memory location
- the object named by the variable is stored in some other location in memory
- like primitives, the value of a class variable is a fixed size
- the value of a class variable is a memory location
- the object whose address is stored in the variable can be of any size
can two reference variables contain the same reference and name the same object?
yes the assignment operator sets the reference (memory address) of one class type variable equal to that of another
what will a change to the object name to a variable do
will produce a change to the object named by the other variable since they are the same object
i.e. if var1 and var2 are referring to the same object and if var1 is change - var2 will also be changed
how does it work when class type variables are store a reference?
- complete definition of the class
- class constructor is called: var = new Class(“Jo”,42);
- creates an object
- places the address of the object in the variable var - let the address be 2056
- var hold 2056 and 2056 holds Jo, 42
how does the assignment operator work with class type variables
- var1 stores 4068
- 4068 stores Lucy, 40
- var1 = var2
- var2 = 4068 (also points to Lucy,40)
- change var2 - var2.set(“Jamie”,35)
- 4068 now holds Jamie, 35
all variables in Java are call-by-value
- a parameters is a local variable that is set equal to the value of the argument
- any change to the parameter cannot change the value of its arguments
how do class parameters behave differently to prim type parameters?
- value plugged into a parameter of class type is a reference (memory address)
- therefore, the parameter becomes another name for the argument
- Any change made to the object named by the parameter (i.e., changes made to the values of its instance variables) will be made to the object named by the argument, because they are the same object
- Note that, because it still is a call-by-value parameter, any change made to the class type parameter itself (i.e., its address) will not change its argument (the reference or memory address)
difference between primitive and class type parameters
- a method cannot change the value of a variable of a primitive type that is an argument to the method
- in contrast, a method can change the values of the instance variables of a class that is an argument to the method.
what does the “=” do with class type variables
produces two variables that name the same object