february 28, 2013 comp 110-003 introduction to programming objects and references haohan li tr 11:00...

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

Upload: patrick-russell

Post on 21-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

February 28, 2013

COMP 110-003Introduction to ProgrammingObjects and References

Haohan LiTR 11:00 – 12:15, SN 011Spring 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

0 0 2 1

2. 1 3 1 2 5 * 0.1

6 6

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)

s

jack

0 1 2 2 3 3 0 5

0 1 0 2 2 8 7 4

U N C i s G

r e a t !

0 3 9 6 3 1 4 7

0 0 0 2 3. 5 0 0

0 0 0 0

J a c k S m i

t h

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

s

jack

0 1 2 2 3 3 0 5

0 1 0 2 2 8 7 4

U N C i s G

r e a t !

0 3 9 6 3 1 4 7

0 0 0 2 3. 5 0 0

0 0 0 0

J a c k S m i

t h

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”);

jacks

apus

0 0 3 2 3 3 0 5

0 0 3 2 2 8 7 4

0 3 9 6 3 1 4 7

0 3 9 7 7 2 2 8

J a v a

J a v a

Example: Books

Book jacksBook = new Book(“Java”);Book apusBook = jacksBook;

jacks

apus

0 0 3 2 3 3 0 5

0 0 3 2 3 3 0 5

0 3 9 6 3 1 4 7 J a v a

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?

Java137

Java253

Java137

Java253

Java509

2078

?

2078

1056

?

?

?

2078

1056

2078

2078

2078

This location of memory is out

of reach – it will be recycled by

the system

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”);boolean strEqual = (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;boolean strEqual = (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”);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

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);

}

}

.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

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

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

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!

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