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
what does the “==” do with class type variables
checks that they have the same memory address
unlike .equals method - doesnt check whether their instance variables have the same values
what is null
specialized constant that may be assigned to a class type variable
It is used to indicate that the variable has no "real value"– It is often used in constructors to initialize class type instance variables when there is no obvious object to use - null is not an object: It is, rather, a kind of "placeholder" for a reference that does not name any memory location– Because it is like a memory address, use ==or !=(instead of equals) to test if a class variable contains null
what is the null pointer exception
- A method cannot be invoked using a variable that is initialized to null– The calling object that must invoke a method does not exist
- Any attempt to do this will result in a “Null Pointer Exception” error message– For example, if the class variable has not been initialized at all (and is not assigned to null), the results will be the same
what does the “new” operator do?
invokes a constructor which initializes an object ad returns a reference to the location in memory of the object created - this reference can be assigned to a variable
what is an anonymous object?
sometimes the object created is used as an argument to a method and never used again - in this case, the object need not be assigned to a variable i.e. given a name
– an object whose reference is not assigned to a variable is anonymous
Double.parseDouble
check with someone?
is using the keyword private enough to prevent privacy leaks?
prim data types - yes class data types - no