Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Lecture 18 Templates, Part II. From Last Time: What is a Template? This is the “official” specification for a template. It says that to define a template.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 1 Program Design
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Array.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
CSC Programming I Lecture 8 September 9, 2002.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays, Type Casting and Constants Engineering 1D04, Teaching Session 5.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Objects and Classes Engineering 1D04, Teaching Session 9.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Copyright © 2002 Pearson Education, Inc. Slide 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
for-each PROS CONS easy to use access to ALL items one-by-one
Variables in Java A variable holds either
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Two Dimensional Arrays and Complex Conditions
Java Primer 1: Types, Classes and Operators
Chapter 7 Part 1 Edited by JJ Shepherd
Creating Objects & String Class
Arrays, For loop While loop Do while loop
Class Structure 16-Nov-18.
Topics Introduction to File Input and Output
Java Programming Arrays
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Class Structure 25-Feb-19.
Java Programming Language
Classes, Objects and Methods
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Topics Introduction to File Input and Output
Presentation transcript:

Records Engineering 1D04, Teaching Session 11

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different types of variables:  Integers (int)  Strings (string)  Floating Point Numbers (float, double)  Boolean Values (bool)  Arrays of int, Arrays of double, etc.  2D Arrays of int, 2D Arrays of double, etc.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng2 Records  In many cases, we have a need to represent data more complex than a single number or a text string.  Arrays allow us to put related values of the same type together, but what if the types of the elements are different?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng3 Alice Eve Bob Charlie A string An array containing 4 strings An integer An array containing 4 integers High Scores  Recall the high score system.  We used two separate arrays to store the following items: The top ten scores The names associated with those scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng4 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob Charlie New score to be inserted here j 1 i Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng5 Alice Eve Bob Charlie New score to be inserted here j 1 i Dave 6000 Before starting, notice that Eve's score is High Scores for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng6 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob Charlie New score to be inserted here 3 j 1 i Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng7 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob New score to be inserted here 3 j 1 i Name moves Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng8 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob New score to be inserted here 2 j 1 i Name moves but score does not! Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng9 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob New score to be inserted here 2 j 1 i Dave High Scores Name moves but score does not!

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng10 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob New score to be inserted here 1 j 1 i No longer true Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng11 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Eve Bob New score to be inserted here 1 j 1 i Dave 6000 ??? High Scores No longer true

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng12 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Dave Eve Bob New score to be inserted here 1 j 1 i Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng13 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Dave Eve Bob New score to be inserted here 1 j 1 i Dave High Scores

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng14 Alice Dave Eve Bob High Scores for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Eve's score is now 4000 (Bob's former score). What happened?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng15 for (j = names.Length - 1; j > i; j--) names[j] = names[j-1]; scores[j] = scores[j-1]; names[i] = name; scores[i] = score; Alice Dave Eve Bob Notice the lack of braces Only the name gets shifted inside of the loop. So the names and scores become mismatched!! High Scores { }

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng16 High Scores  Keeping two separate arrays of associated data is potentially dangerous.  We have to ensure that the data in one array is always synchronised with the data in the other.  Keeping the two arrays synchronised requires additional code.  A bug like the one you just saw could easily result in names matching the wrong scores.  If we want to change the number of scores, we have to change the size of two arrays.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng17 High Scores  We can only use a single array if we can store more complex data in an array.  Maybe we could store a string like "Alice 7000 points"  It becomes inconvenient to extract the score portion of the string to compare it with other scores when inserting.  We need an easy way to treat multiple pieces of data as a single cohesive object.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng18 Records  A common concept in programming languages is that of records.  A record is several pieces of related data stored together in a single place.  name, address and phone number of a person  name and score of someone on a highscore list  title, track, album and artist of a music file

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng19 A record, containing a string and an integer. A string Alice7000 An integer Records  When dealing with records, all of the information in a record moves around together as a single unit.  Consider a record containing a high score and the name of the person it belongs to.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng20 A record, containing name and score. An array containing 4 records. Alice7000 Eve5000 Bob4000 Charlie Records  We could then make a single array containing a number of these records.  Our highscore list might then look like this

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng21 Records  Because names and scores are bound together in a single package it's not possible for them to fall out of synchronisation with each other.  The single packaging means that both the name and score can be moved with a single statement.  This becomes increasingly useful as the number of items in the record increases from two to many more.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng22 Records  There are clearly many possibilities for combinations of different types of data to store in a record.  Before storing data in a record you need to define its type.  Once you have declared the type, you can create many records of this type (for example, one for each person in the high score list).

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng23 Records  When declaring variables in any program you specify two things about each variable.  Its type  Its name Examples:  int counter;  type is integer, name is "counter".  string password;  type is string, name is "password".

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng24 Records  When declaring variables in a record you do exactly the same thing.  You must specify the type and the name of each variable in the record. This defines the type of the whole record.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng25 Records  When declaring variables in a record you do exactly the same thing.  You must specify the type and the name of each variable in the record. This defines the type of the whole record.  Assuming a high score entry consists of a string for name and an integer for score, we want:  string name;  int score;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng26 Records  The idea of a record is to wrap up its components into a single unit.  The string and the integer in the high score entry record exist as a pair.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng27 Records  In C#, the class keyword is used for many things.  In general, the class keyword defines a template for the creation of objects.  A record is a specific sort of object but everything mentioned here about records will be applicable to objects in general.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng28 class HighScoreEntry { public string name; public int score; } Records  One purpose of the class keyword is to define a record type. public indicates we need to access these items from outside of the class - we still need to discuss public and private in more detail - stay tuned

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng29 new HighScoreEntry(); Records  After declaring a record type with a given name we use the new keyword to create a new instance of it.  new returns a Reference to the new record.  The concept of references is very important!  To create a new HighScoreEntry:

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng30 new HighScoreEntry(); When the new statement runs a new record instance is created in memory. Records  Creating a new record instance

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng31 Records  Creating a new record instance new HighScoreEntry(); When the new statement runs a new record instance is created in memory.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng32 The new instance has no name. It just “floats” in memory. Records  Creating a new record instance new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng33 Records  Creating a new record instance The new statement, however, returns a reference to the new object. new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng34 A reference can be thought of as an arrow or a pointer indicating the location of an object. Records  Creating a new record instance new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng35 Records  new returns a reference to the new record.  We can follow this reference to find the record.  A reference is its own type of value (typically the memory address of the record).

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng36  We can create variables to store references.  This creates a variable called myRef that is capable of storing a reference to a HighScoreEntry record.  This variable can not store references to other types of records. HighScoreEntry myRef; Records

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng37 HighScoreEntry myRef; myRef = new HighScoreEntry(); Records  Naturally, a reference variable can be used to store the reference that is returned by new.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng38 HighScoreEntry myRef; myRef = new HighScoreEntry(); Just like with any other variable declaration a new named box is created in memory. This box is able to store HighScoreEntry references. Creating New Records – Demo myRef

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng39 HighScoreEntry myRef; myRef = new HighScoreEntry(); myRef A new HighScoreEntry record is created (floating in memory, without a name). Creating New Records – Demo

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng40 HighScoreEntry myRef; myRef = new HighScoreEntry(); myRef The reference returned by new is stored in the box named myRef. Creating New Records – Demo

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng41 HighScoreEntry myRef; myRef = new HighScoreEntry(); myRef Even though the record itself is floating without a name, we can follow the reference to access it. Creating New Records – Demo

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng42 Records  For records to be useful we need to be able to inspect and modify (access) their contents.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng43 Records  For records to be useful we need to be able to inspect and modify (access) their contents.  For this purpose we use the. (dot) operator.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng44 class HighScoreEntry { public string name; public int score; } Records  Recall the format of the record definition.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng45 Our record has two elements name (a string) score (an integer) score (integer) name (string) Records  Recall the format of the record definition. class HighScoreEntry { public string name; public int score; }

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng46 Records  Given a reference to a record, we can access each of its individual elements (name or score, for example) by using the dot syntax.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng47 myRef score (integer) name (string) Records  Given our new record from before...

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng48 myRef score (integer) name (string) myRef.name = "Alice"; myRef.score = 7000; Records  Given our new record from before...

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng49 myRef Alice score (integer) name (string) Records  Given our new record from before... myRef.name = "Alice"; myRef.score = 7000;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng50 myRef Alice7000 score (integer) name (string) Records  Given our new record from before... myRef.name = "Alice"; myRef.score = 7000;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng51 myRef Alice7000 score (integer) name (string) Records  Given our new record from before... myRef.name = "Alice"; myRef.score = 7000;

A Quick Summary © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng52

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng53 A Quick Summary  The class keyword defines the layout of a record type.  The word "class" is used like "category" to define a group of similar objects.  A class is a kind of template for creating new objects of that type.  Objects of a specific type are said to belong to that class.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng54 A Quick Summary  Records are data types that can contain several different elements.  We can make new records (object instances) using the new keyword.  A record is a specific kind of object.  A variable (named memory location) can contain a reference to an object. An object itself has no name.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng55 A Quick Summary  But seriously, what's the deal with references?  Isn't this “new” business just a really strange way of declaring a variable?

Object References © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng56

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng57 int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b c Object References  Recall the introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng58 5 int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b c Object References  Recall introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b c Object References  Recall introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b 3 c Object References  Recall introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b 3 c Object References  Recall introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng int a, b, c; a = 5; b = a; c = 3; a = 7; c = b + a; a b 12 c Object References  Recall introduction to variables.  Each variable is a specific, named place in memory where a value is stored.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng63 HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7; b a Object References  We can draw a similar diagram for reference types.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng64 b a Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng65 b a 5 Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng66 b a 5 What happens next? Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng67 b a 5 Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng68 b a 5 Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7; a and b now both refer to the same record!

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng69 b a 7 Object References  We can draw a similar diagram for reference types. HighScoreEntry a, b; a = new HighScoreEntry(); a.score = 5; b = a; b.score = 7; a and b really do refer to the same record!

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng70 Object References  With references it's possible to have two reference variables referring to the same object.  In the example, a and b both refer to the same object.  Any changes made via a (a.score, a.name) will change b as well. Any change made via b will change a as well.

Objects – Things to Consider © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng71

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng72 Objects – Things to Consider  What happens if we have no references to an object?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng73 a Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng74 a Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng75 a 5 Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng76 a 5 Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng77 a 5 The reference to the old entry is replaced by a reference to the new entry Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry();

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng78 a 5 Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry(); We have no way to reference the entry with the 5 in it What happens to it?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng79 Objects – Things to Consider  Left to itself, the object would just sit around consuming memory.  But fortunately, C# has a language feature called Garbage Collection.  When records are no longer accessible they are garbage collected (since nobody will notice that they are missing anyway).  All memory associated with the record is freed for other uses.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng80 a 5 Objects – Things to Consider  Take this example HighScoreEntry a; a = new HighScoreEntry(); a.score = 5; a = new HighScoreEntry(); We have no way to reference the entry with the 5 in it It gets garbage collected. Most importantly - we have no way of using the data stored in this record!

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng81 High Scores Yet Again DECLARATIONS REQUIRED class HSRecord { public string name; public int score; } const int maxElements = 6; HSRecord[] highScore = new HSRecord[maxElements]; int INhighScore = 0; //Num elements in //highScore

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng82 High Scores Yet Again  AddScores with objects... mark = 0; while (mark <= INhighScore - 1 && highScore[mark].score >= newScore) mark++; if (mark <= INhighScore - 1) { for (j = INhighScore - 1; j > mark; j--) { highScore[j] = highScore[j - 1]; } highScore[mark] = newRecord; } moves both score and name in a single instruction note: it is moving the references to the score and name! careful not to “lose” the reference to the new record

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng83 Objects – More Things to Consider  What is contained in a reference that doesn't (yet) point to a valid record?  What happens if you try to access the non- existent record pointed to by such a reference?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng84 Objects – More Things to Consider  New references are initialised to null until you assign a new value to them.  "null" means that the reference variable refers to no object.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng85 MyClass myRef; myRef = null; myRef.value = 10; Objects – More Things to Consider  But what happens if you try to access a null reference?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng86 Objects – More Things to Consider myRef = new myRecord(); is missing equivalent to myRef = null; compile error

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng87 Objects – More Things to Consider  It is not always possible to detect null references at compile time.  After a null reference exception occurs your program crashes and stops running.  Sometimes we won't know if a reference variable is null or not, so how do we handle that?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng88 MyClass myRef; if (myRef != null) myRef.value = 10; Objects – More Things to Consider  Where necessary, we can avoid crashes by checking our reference variables to ensure that they are non-null before accessing them.