Review: Using Imported Code Class #06: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 30 Jan. 2018 ; = new ();.setbackground( java.awt.color.blue );.setforeground( java.awt.color.yellow );.turnby( 90 );.turnby( 90 );.turnby( 90 ); import java.awt.color; ; = new (); This adds the import command to the class, and now we don t have to use the full address for our colors These programs produce exactly the same result on-screen when they run.setbackground( Color.blue );.setforeground( Color.yellow );.turnby( 90 );.turnby( 90 );.turnby( 90 ); Tuesday, 30 Jan. 2018 Software Design I (CS 120) 2 What About the? If we want to use built-in Java classes like Color, we need to either refer to their full class address or import For classes that aren t built in to the language, there is another simple option: put all the class files into the same source directory (folder) When we run our program, the JVM will allow us to use any class of object that we address, import, or that it can find along-side the source code for the main() method Review: Classes and Object Instances cil; cil = new (); We are working with both a class () and an object (actual code and data saved in memory, to which the name cil refers) We call the object an instance of the class Within a single program we can have more than one instance of a single class (or type of object) Each instance will be declared and instantiated separately, using distinct names Tuesday, 30 Jan. 2018 Software Design I (CS 120) 3 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 4 1
Review: Creating an Object Often, we declare and instantiate objects separately, using two distinct Java commands: cil; cil = new (); Often, however, we do this together, in a single line: cil = new (); Using Multiple Objects ; = new (); cil = new ();.turnclockwise;.moveforward; cil.turncounterclockwise; cil.moveforward; One object is declared and instantiated (separately) Second object is declared and instantiated (all at once) Each object has its methods run separately, using its own unique identifier (name) Within a single code block, which approach we choose is often optional, since both do the same thing We will later learn about cases where this is not true Our code doesn t always need to use a single object It may be convenient (or necessary) to use multiple objects of the same (or different) type Tuesday, 30 Jan. 2018 Software Design I (CS 120) 5 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 6 Review: What a Declaration Means A variable declaration tells the JVM/CPU to create a new identifier (name) in its memory-space Note: Eventually, we will want the variable to be the name of an actual object in some memory location. At first, however, the variable points to no actual memory location. (No memory has been set aside for an object yet, just the name.) The variable has a null reference. ; Memory null Tuesday, 30 Jan. 2018 Software Design I (CS 120) 7 What Instantiation Means A variable instantiation tells the JVM/CPU to: 1. Go to variable (in memory), and find another empty location in memory for it to point to (where we will store the actual object) 2. Now, create a new object and fill up the newly made slot in memory with the required data 3. Point the variable that memory location, so you can find the object later (by storing the address of the object) NOTE: to help make sure our code is free of bugs, the compiler will check that the type of object we are creating agrees with the type of variable that we declared. Here, e.g., has type. = new (); Space for Memory Tuesday, 30 Jan. 2018 Software Design I (CS 120) 8 2
The Importance of Initialization Variable Assignment Once we have actually instantiated an object of type, we can do things with it by making method calls What haps when we try to call methods on objects that do not exist yet? The code at right will not compile! ;.moveforward(); Error: is never instantiated, but a method is called on it! Giving a variable an actual value for the first time is called initialization Once we declare and instantiate a variable, we can change its value This is called variable assignment, and has the basic syntax: objectname1 = objectname2; = new (); cil; In Java this is read a very specific way: the identifier on the left now points to whatever object is given by the expression on the right When we are dealing with objects that are instances of a class (as in this example code), this moves the reference of identifier objectname1 It now points to the same memory location as objectname2 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 9 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 10 What Assignment Means A variable assignment tells the JVM/CPU: 1. Take the name on the left 2. Change what it refers to so that it is now the same as thing named on the right = cil; cil Variable Assignment This will all work just fine, as long as objectname1 and objectname2 are both variables of the same type (that is, their classes must agree with one another): = new (); cil; Question: what haps to the original object that used to name? Answer: it is orphaned, and no longer exists for use in our program Space for Space for If we try to do this with types that don t agree, the code will not compile at all: = new (); Color col = java.awt.color.blue; = col; // compiler error!! Tuesday, 30 Jan. 2018 Software Design I (CS 120) 11 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 12 3
Variables and Their Objects When we properly instantiate an object: ; = new (); The variable name ( ) is bound to its object Each variable can only be bound to one object at a time Before the initialization on the 2 nd line of code above, the variable is bound to no object (it is null) We can also un-bind a variable so it points at no object: The that used to be bound is now an orphan Compilation Errors and Runtime Errors Many of the mistakes a coder makes will be caught when they try to compile their code, before it can ever be run Bad syntax (missing semicolons, etc.) Type errors (the wrong input parameter for a method, etc.) Even more complex things However, some errors cannot be caught at compile time Some code will compile, but then fail when it actually runs These are called runtime errors (or runtime exceptions) The Java environment (JVM) will try to signal what kind of runtime error we have, using an exception message Tuesday, 30 Jan. 2018 Software Design I (CS 120) 13 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 14 Anatomy of a Runtime Error Lines starting with at tell us where the error was made Look for your file names and line numbers In Eclipse, you can click on line number to go there in the code These error messages can be quite complex! They are meant to be helpful, however. The first line tells us what sort of error we have made This is a common type: Null Pointer Exception Tracing Objects, cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; It is often useful to keep track of the objects that are bound to variables This helps in figuring out what some code does, and in de-bugging code that is not working properly If you start getting a lot of Null Pointer errors, for example, you may find doing a program trace can be very helpful! Tuesday, 30 Jan. 2018 Software Design I (CS 120) 15 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 16 4
Tracing Objects with Object Diagrams (1) Tracing Objects with Object Diagrams (2), cil, ; = new (); Note:.moveForward(); to save typing, Java allows us.turnclockwise(); to declare multiple variables of cil = new (); the same type on one single line. The syntax is the basically same, but we can have more than one variable = cil; name (separated by commas) after the single variable type. cil, cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tuesday, 30 Jan. 2018 Software Design I (CS 120) 17 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 18 Tracing Objects with Object Diagrams (3), cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tracing Objects with Object Diagrams (4), cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tuesday, 30 Jan. 2018 Software Design I (CS 120) 19 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 20 5
Tracing Objects with Object Diagrams (5) Tracing Objects with Object Diagrams (6), cil, ; = new ();, cil, ; = new ();.moveforward();.moveforward();.turnclockwise(); cil = new (); cil.turnclockwise(); cil = new (); cil = cil; = cil; Tuesday, 30 Jan. 2018 Software Design I (CS 120) 21 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 22 Reminder: Orphans are Unavailable Forever After the first object is let go, so no variable name is bound to it, we cannot ever use it again This allows the JVM to free up some memory space, which can be useful in large, complex programs Don t do it by accident! cil = new (); cil = new (); = cil; This Week & Next Meetings this week: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Homework 01: Groundhog image Friday, 02 Feb., 5:00 PM, D2L Reading assignment: Chapter 3, sections 1 10 Next Monday, 05 Feb., by start of class Quiz 01: Covers lectures 01 04 (all on Notes page) Monday, 05 Feb. (end of class) Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM Tuesday, 30 Jan. 2018 Software Design I (CS 120) 23 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 24 6