Download presentation
Presentation is loading. Please wait.
Published byPatrick Russell Modified over 9 years ago
1
February 28, 2013 COMP 110-003 Introduction to Programming Objects and References 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 0021 2.13125* 0.1 66
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) s jack 01223305 01022874 UNC isG reat! 03963147 00023.500 0000 JackSmi th
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 s jack 01223305 01022874 UNC isG reat! 03963147 00023.500 0000 JackSmi th
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”); jacks apus 00323305 00322874 03963147 03977228 Java Java
13
Example: Books Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; jacks apus 00323305 00323305 03963147Java
14
Objects in Memory jacksBook apusBook ???? Memory Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBook now has 509 pages! ???????? ? Java ? Java ? Java ? Java ? Java 137 Java 253 Java 137 Java 253 Java 509 2078 ? 2078 1056 ???? ? 2078 1056 2078 This location of memory is out of reach – it will be recycled by the system
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”); boolean strEqual = (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; boolean strEqual = (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”); boolean strEqual = (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 public class Book { private String name; private int page; public boolean 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 public void increaseNum(int num) { num++; // num is changed } public void doStuff() { 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 public void changeBook(Book book) { book = new Book(“Biology”); // book is changed } public void doStuff() { 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 public void changeBook(Book book) { book.setName(“Biology”); // who is changed? } public void doStuff() { 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 book is 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.