references Flashcards

1
Q

a computer has two forms of memory

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what does main memory consist of

A

long list of numbered locations called bytes - each byte is 8 bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the number called that identifies a byte

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what happens if more than one byte is needed to store something

A

several adjacent bytes are used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is memory location?

A

an entire chunk of memory that holds the data is called memory location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what happens when a variable is in primitive type?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what happens when the variable is a class type?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can two reference variables contain the same reference and name the same object?

A
yes
the assignment operator sets the reference (memory address) of one class type variable equal to that of another
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what will a change to the object name to a variable do

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how does it work when class type variables are store a reference?

A
  1. complete definition of the class
  2. class constructor is called: var = new Class(“Jo”,42);
  3. creates an object
  4. places the address of the object in the variable var - let the address be 2056
  5. var hold 2056 and 2056 holds Jo, 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how does the assignment operator work with class type variables

A
  1. var1 stores 4068
  2. 4068 stores Lucy, 40
  3. var1 = var2
  4. var2 = 4068 (also points to Lucy,40)
  5. change var2 - var2.set(“Jamie”,35)
  6. 4068 now holds Jamie, 35
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

all variables in Java are call-by-value

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how do class parameters behave differently to prim type parameters?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
difference between primitive and class type 
parameters
A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does the “=” do with class type variables

A

produces two variables that name the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what does the “==” do with class type variables

A

checks that they have the same memory address

unlike .equals method - doesnt check whether their instance variables have the same values

17
Q

what is null

A

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
18
Q

what is the null pointer exception

A
  • 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
19
Q

what does the “new” operator do?

A

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

20
Q

what is an anonymous object?

A

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

21
Q

Double.parseDouble

A

check with someone?

22
Q

is using the keyword private enough to prevent privacy leaks?

A
prim data types - yes
class data types - no