240 likes | 388 Views
COMP 110-003 Introduction to Programming Objects and References. February 28, 2013. Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013. Announcements. The deadline of Program 3 is extended to March 10, Sunday The midterm is on next Thursday The sample exam and its solution is online
E N D
COMP 110-003 Introduction to Programming Objects and References February 28, 2013 Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013
Announcements • The deadline of Program 3 is extended to March 10, Sunday • The midterm is on next Thursday • The sample exam and its solution is online • The sample exam is difficult. The actual exam will be a bit easier • Focus on lecture notes, worksheets and assignments • You will receive the grade for Lab 3 and Program 2 by next Tuesday
Review • Classes • Objects • Instance variables • Methods • Return types • Parameters and arguments • Information hiding and encapsulation • public/private • accessors/mutators
Variables of a Class Type • Behave differently from variables of a primitive type • Scanner keyboard = new Scanner(System.in); • Student jack = new Student(); • String unc = “UNC is great!”; • At least, you have seen that you can not easily compare two strings • string1 == string2; //BAD • string1.equals(string2); //GOOD
Primitive Variables and Memory • When declaring a primitive variable, a certain amount of memory is assigned/allocated based on the declared primitive type int age;double length;char letter; main memory
Primitive Variables and Memory • The actual data values are saved in the allocated memory for primitive types int age;double length;char letter; main memory
Variables of a Class Type • What goes in these variables? String s;Student jack; main memory
Variables of a Class Type • What goes in these variables? • In a class type variable, the address pointing to the actual object is saved (not the object itself) sjack
Variables of a Class Type • Contain the memory address of the object named by the variable • NOT the object itself • Object is stored in some other location in memory • The address to this other location is called a reference to the object • Class types are also called reference types
Reference Types • Reference types vs. data types sjack
Example: Books • Assume that we have a class named Book Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); vs. Book jacksBook = new Book(“Java”); Book apusBook = jacksBook;
Example: Books Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); jacksapus
Example: Books Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; jacksapus
Objects in Memory Memory Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBooknow has 509 pages! jacksBook apusBook 2078 1056 2078 2078 ? ? 2078 ? This location of memory is out of reach – it will be recycled by the system ? ? ? 2078 1056 2078 Java 253 Java 137 ? ? Java ? ? ? ? ? Java ? Java ? Java ? Java 137 Java 253 Java 509
Remember • Variables of a class type contain memory addresses • NOT objects themselves • It is dangerous to use assign operator “=” and equal-to operator “==” on class type variables
== vs. equals() for Strings Explained • String is a class type • What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); booleanstrEqual = (s1 == s2); • strEqual is false! Why? • s1 and s2 store different addresses!
== vs. equals() for Strings Explained • String is a class type • What happens when you have String s1 = new String(“Hello”); String s2 = s1; booleanstrEqual = (s1 == s2); • strEqual is true! • s1 and s2 have the same address!
== vs. equals() for Strings Explained • String is a class type • What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); booleanstrEqual = (s1.equals(s2)); • strEqual is true! Why? • String’s .equals() method checks if all the characters in the two Strings are the same
Writing the .equals() method publicclass Book { private String name; privateint page; publicboolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); } }
.equals() • Every class has a default .equals() method if it is not explicitly written • It use “==” to check every pair of instance variables • You decide what it means for two objects of a specific class type to be considered equal • Perhaps books are equal if the names and page numbers are equal • Perhaps only if the names are equal • Put this logic inside .equals() method
Parameters of a Primitive Type publicvoidincreaseNum(int num) { num++; // num is changed } publicvoiddoStuff() { int x = 5; increaseNum(x); System.out.println(x); } • Prints 5. Why? • num is local to increaseNum method; does not change x
Parameters of a Class Type publicvoidchangeBook(Book book) { book = new Book(“Biology”); // book is changed } publicvoiddoStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } • Prints Java. Why? • book is local to changeBook, does not change jacksBook
Parameters of a Class Type publicvoidchangeBook(Book book) { book.setName(“Biology”); // who is changed? } publicvoiddoStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } • Prints Biology. Why? • book contains the same address as jacksBook! • Pay attention: the value of bookis not changed!
Take-Home Message • Class type variable is a pointer to the actual object • Be extremely careful when you use “=”, “==” to operate class type variables, or passing them to a method