Chapter 3 - Primitives and References Flashcards
Variables come in what two flavors?
Primitive and Object Reference
What are local variables
Variables declared within a method
What are arguments
Values sent to a method by the calling code
What are return types
Values sent back to the caller of the method
Primitives hold?
Fundamental Values (ints, booleans, floats)
Use the dot operator on a reference variable to say?
Use the thing before the dot to get me the thing after the dot
Think of the dot operator as
pressing a button on a remote
An object reference variable is
full of bits representing a way to get to the object
Three steps of object declaration, creation and assignment
Declare a reference variable ... Dog myDog Create an object ... new Dog(); Link the object and the reference ... Dog myDog = new Dog();
Think of a dog reference variable as
a Dog remote control. Use it to get the object to do something (invoke methods)
What’s a null reference
A reference variable that doesn’t refer to anything
Arrays are always
Objects
Declare a dog array variable
Dog [] pets;
What’s a primitive variable value
The bits representing the value
What’s a reference variable value
The bits representing a way to get to an object on the heap
A reference variable is like?
A remote control
When does a reference variable have a null value
When it’s not referencing any object
Arguments
Values sent to a method by the calling code
Return types
Values sent back to the caller of the method
Think of a variable as?
A coffee cup. It holds something. It has a size and type
A variable needs a name so
you can use that name in code
myDog.bark() means
use the object referenced by the variable myDog to invoke the bark() method.
An object reference is just
another variable value
Primitive variable
byte x = 7;
The bits representing 7 go into the variable
Reference variable
Dog myDog = new Dog(); The bits representing a way to get to the Dog object go into the variable
An array is like?
A tray of cups
Every element in an array is just a?
A variable. Primitive or Reference
In a dog array each element can hold
a remote control to a Dog.
When the dog is in an array we don’t have?
an actual variable name. Use array notation and push the remote on an object a particular index in the array.
Dog[] myDogs = new Dog[3]; myDogs[0] = new Dog(); myDogs[0].name = "Fido" myDogs[0].bark();