html5-img
1 / 24

February 28, 2013

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

chaka
Download Presentation

February 28, 2013

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110-003 Introduction to Programming Objects and References February 28, 2013 Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013

  2. 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

  3. Review • Classes • Objects • Instance variables • Methods • Return types • Parameters and arguments • Information hiding and encapsulation • public/private • accessors/mutators

  4. 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

  5. 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

  6. 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

  7. Variables of a Class Type • What goes in these variables? String s;Student jack; main memory

  8. 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

  9. 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

  10. Reference Types • Reference types vs. data types sjack

  11. 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;

  12. Example: Books Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); jacksapus

  13. Example: Books Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; jacksapus

  14. 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

  15. 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

  16. == 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!

  17. == 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!

  18. == 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

  19. 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); } }

  20. .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

  21. 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

  22. 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

  23. 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!

  24. 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

More Related