Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M250 Unit 3 Subsection 2 - Data types and variables (reference) (12)

Matt
January 12, 2020

M250 Unit 3 Subsection 2 - Data types and variables (reference) (12)

Matt

January 12, 2020
Tweet

More Decks by Matt

Other Decks in Education

Transcript

  1. We’ve seen that primitive type variables are variables that hold

    values of a primitive data type. Reference type variables are variables that hold a reference to an object. As we did with primitive variables, we often abbreviate “reference type variable” to just “reference variable” in order to keep things concise.
  2. We can think of a reference as an address for

    where an object is in memory. And so a reference variable has a value of this address. That is, a reference variable holds the location of an object as a value. So to access an object, perhaps to send it a message, we actually need to access the reference variable storing the object’s location. And this is because that reference variable is the only thing that knows where the object is. But please note that as we go deeper into the course this distinction gets blurred. We tend to stop talking about a reference variable as holding a reference value (the object’s location in memory) and instead talk more about the reference variable as if it were the object itself. And this blurring is natural and okay to do. But we should be mindful that a reference variable is technically not the object. It is a variable holding reference to the object’s location in memory.
  3. Declaring a reference variable Like primitive variables, to declare a

    reference variable we need to write: • A type • An identifier The only difference is that its type isn’t a primitive data type (like int, double, boolean, or char), but a reference type. So what is a reference type? A reference type tells the compiler what kind of objects we want a new reference variable to hold reference to. So if we know we want a variable called kermit to hold reference to a Frog object, then the reference type of kermit would be Frog. Similarly, if we wanted trevor to hold reference to a Toad object, then the reference type of trevor would be Toad.
  4. So we can see that the reference type of a

    variable is simply the name of the class of the object that we want it to hold reference to. We declare reference variables like this: reference type identifier; e.g. Frog kermit; - Let kermit hold a reference to an instance of Frog. Toad trevor; - Let trevor hold a reference to an instance of Toad. HoverFrog gribbit; - Let gribbit hold a reference to an instance of HoverFrog. Account myAccount; - Let myAccount hold a reference to an instance of Account.
  5. But note that simply declaring a reference variable is not

    the same as assigning it an object to reference, just as declaring a primitive variable is not the same as assigning it a value. Executing Frog kermit; doesn’t create a Frog object. It merely creates a reference variable of type Frog called kermit, to which we could assign an object. Without doing this, kermit has little-to-no use, because it doesn’t hold a reference to any object. Before we talk about assignment any more, we have to talk about the keyword new.
  6. The keyword new Java has keywords. Keywords are specific words

    that Java uses to do things. We can’t use the name of a keyword as an identifier – like as a variable name or a message name. new is a keyword that is used to create objects. To do this we execute a statement consisting of: • new • A constructor For now, don’t worry about what a constructor is as we’ll come back to it in the next unit. For now I just want you to remember two things to help us along: 1] A constructor’s name is the same as the name of the class of object we want to create 2] That a constructor is always followed by brackets even when there’s nothing to put in them.
  7. So say I want to create an instance of a

    class. I would execute a statement consisting of: new constructor(); E.g. • new Frog(); - creates a Frog object. • new Toad(); - creates a Toad object. • new HoverFrog(); - creates a HoverFrog object.
  8. So we can create objects which is great. But executing

    a statement like new Frog(); wouldn’t give us a way of accessing the new Frog object we’d have just made. We’d have created the object for sure, but we’d have no reference to where it existed in memory. This is where reference variables come into play. A reference variable holds a reference to an object’s location in memory. So we can: 1] Declare a reference variable 2] Create an object 3] Store that object in the reference variable
  9. Assigning a reference type variable Just like with primitive variables,

    assignment of reference variables is done with the use of the assignment operator = We could execute: Frog kermit; kermit = new Frog(); Line 1 declares a reference variable of type Frog called kermit. Line 2 creates a new instance of Frog and assigns it to kermit.
  10. We can even do this in one line, like we’ve

    seen done with primitive variables: Frog kermit = new Frog(); Step-by-step this says: “create a new reference variable of type Frog and call it kermit, also create a new Frog object and store within kermit the location of this object within memory.” After executing this we can see that sending messages to kermit communicates with the Frog object we just created and assigned to it. Messages like left(), right(), and setPosition() can be sent to kermit because kermit is the reference variable for a Frog object. If we had only created the Frog object, we couldn’t send it messages because there would be no reference to where it was located in memory.
  11. Remember that reference variables differ from primitive variables in some

    ways. We can’t do arithmetic with them. For example, we can’t add kermit to gribbit or multiply kermit by 2.
  12. Garbage collection Let’s say we created created a new Frog

    object and assigned it to kermit: Frog kermit = new Frog(); Then we set kermit’s colour to brown and move it right: kermit.brown(); kermit.right(); So kermit’s state is now: colour is brown, position is 2. What if we then executed: kermit = new Frog(); What is kermit’s state now? Well we just assigned kermit to a new Frog object. And as we’ve seen new Frog objects are initialised to have a position of 1 and a colour of green. So kermit now references a newly created Frog object, so its state is now: colour is green, position is 1.
  13. So what happened to our first Frog object? It’s gone.

    An object is only useful if we have a way of communicating with it. Because we assigned kermit to a new object, the original object has no variable referencing it. So this original object (the brown one) is available for garbage collection. Garbage collection is a process carried out by the JVM to delete objects that have no variables referencing them. This is done to save space. So the original object is gone. If we remove every reference to an object, there’s no way to get it back and it gets garbage collected.
  14. Null value Here we discuss the literal ‘null’. If we

    don’t want a reference variable to hold reference to an object, we can assign it the literal value ‘null’. Assuming x has been declared to be a reference variable we could execute something like x = null; This means the reference variable x (of some type) exists but doesn’t hold a reference to an object. This also happens when we only declare a reference variable (and don’t assign it a value) – e.g. executing Frog kermit; results kermit holding the value null. Null is of limited use, since we can’t send messages to variables that hold a value of null - because there is no object to send messages to, the variable literally doesn’t reference one.
  15. Question What’s the result of executing each piece of code

    separately? – Toad pip; pip = new Frog(); – Frog tinyTim = new Toad(); – Frog kermit = new Frog;
  16. Toad pip; pip = new Frog(); The first statement executes

    correctly and creates a reference variable of type Toad called pip. However, the second statement fails because we’ve tried to assign a Frog object to a variable of type Toad.
  17. Frog tinyTim = new Toad(); The entire statement fails to

    execute. We’ve tried to create a Toad object and assign it to a new reference variable called tinyTim. But tinyTim is of reference type Frog and not Toad, so it can’t hold a reference to Toad objects.
  18. Frog kermit = new Frog; The statement fails to execute.

    What is the purpose of the word Frog after the word new? It’s a constructor. As we’ve said, all constructors require a pair of brackets following them even if there’s nothing in the brackets. The statement should be: Frog kermit = new Frog();