Transcript lect21
COMP1681 / SE15
Introduction
to Programming
Lecture 21
Objects and References
This week’s Learning Objectives
To review the notion of a reference.
To be aware of the implications of using references
Revisit Arrays
SE15: Objects and References
21–2
Lecture Outline
Memory allocation of different types
What does new do?
Implications of using references
Defining equals methods
Savitch section 4.3
SE15: Objects and References
21–3
Variables of Primitive Types
Every variable, whether a class type or a primitive type,
is implemented as a memory location.
For primitive types, the value of the variable is stored in
the memory location assigned to the variable.
int score = 10;
char symbol = ‘a’;
score
10
symbol
SE15: Objects and References
a
Two memory
Locations for the
two variables
(known size)
21–4
Class variables
For class variables, then the object named by the variable is stored
in some other location in memory, and the memory address of
where the object is located is stored in the variable that names the
object.
The memory address of where the object is located is called a
reference to the object
Book myBook1;
Book myBook2;
myBook1
myBook2
?
?
Two memory
Locations for the
two variables
:
SE15: Objects and References
21–5
What’s new?
Book myBook1;
At this point your program has a place to store a memory address, but
no where to store the data in the instance variables.
new assigns a memory location for an object.
myBook1 = new Book();
assigns a memory location for a Book object and places the memory
address of that location in the variable myBook1.
Informally new creates the instance variables of the object
SE15: Objects and References
21–6
References to objects
Book myBook1 = new Book();
Book myBook2 = new Book();
myBook1
3000
myBook2
1052
:
1052
3000
SE15: Objects and References
?
?
?
?
Two memory
Locations for the
two objects
(could be any value)
21–7
References to objects
myBook1.setTitle(‘Macbeth’);
myBook1.setAuthor(‘Shakespeare’);
myBook1
3000
myBook2
1052
myBook2.setTitle(‘Programming’);
myBook2.setAuthor(‘Pickle’);
1052
3000
:
Programming
Pickle
Macbeth
Shakespeare
SE15: Objects and References
21–8
Assignment (=) with primitive types
int score1 = 10;
int score2 = 34;
score1
10
score2
34
:
score2 = score1;
score1
10
score2
10
:
SE15: Objects and References
21–9
Assignment (=) with Class variables
myBook2 = myBook1;
myBook1
3000
myBook2
3000
:
1052
3000
Programming
Pickle
Macbeth
Shakespeare
SE15: Objects and References
21–10
myBook2.setTitle(‘Java’);
myBook2.setAuthor(‘Savitch’);
myBook1
3000
myBook2
3000
:
myBook1 and myBook2 are now
two names for the same object!
1052
3000
SE15: Objects and References
Programming
Pickle
Java
Savtich
21–11
Equality (==) with variables of a
Primitive type
int a = 10;
int b = 10;
if (a == b)
System.out.println(“They are EQUAL”);
else
System.out.println(“They are NOT equal”);
This will produce the output
They are EQUAL
SE15: Objects and References
21–12
Equality (==) with variables of a
Class type
Book myBook1 = new Book();
Book myBook2 = new Book();
myBook1.setTitle(‘Java’);
myBook2.setTitle(‘Java’);
myBook1.setAuthor(‘Savitch’);
myBook2.setAuthor(‘Savitch’);
if (myBook1 == myBook2)
System.out.println(“They are EQUAL”);
else
System.out.println(“They are NOT equal”);
This will produce the output
They are NOT equal
SE15: Objects and References
21–13
Why are they NOT equal?
myBook1.setTitle(‘Java’);
myBook1.setAuthor(‘Savitch’);
myBook1
3000
myBook2
1052
myBook2.setTitle(‘Java’);
myBook2.setAuthor(‘Savitch’);
:
1052
Because 1052 is not equal to 3000
(The == operator only checks if the
memory addresses are the same)
3000
SE15: Objects and References
Java
Savitch
Java
Savitch
21–14
Defining an equals method
•
When you compare two objects using the == operator you are
checking to see if they have the same memory address.
•
You are NOT testing for what you would intuitively call “being equal”.
•
To do this you should define a method equals
SE15: Objects and References
21–15
An equals method for a Date class
public class Date
{
private int year;
private int month;
private int day;
public boolean equals(Date otherDate)
{
if((year == otherDate.year)
&& (month == otherDate.month)
&& (day == otherDate.day))
return true;
else
return false;
}
}
SE15: Objects and References
21–16
References and Arrays
For Primitive types
To create a collection of seven variables of type double
double[] temperature = new double[7];
For Class types
To create a collections of seven objects of type Book
Book[] libraryBook = new Book[7];
for( i=0; i < 7; i++)
libraryBook[i] = new Book();
SE15: Objects and References
21–17
Arrays and references
libraryBook[0]
?
libraryBook[0]
2678
libraryBook[1]
?
libraryBook[1]
2426
libraryBook[2]
libraryBook[2]
libraryBook[3]
?
:
?
2380
:
libraryBook[4]
?
libraryBook[5]
libraryBook[6]
?
2380
Java
Savitch
?
2426
Macbeth
Shakespeare
2678
Programming
Pickle
SE15: Objects and References
21–18
NullPointerException
No doubt you have all seen this error!
It indicates your code tried to access some member of a
class variable, but the class variable names no object
(it does not contain a reference to any object)
Book[] libraryBook = new Book[7];
libraryBook[0].setAuthor(‘Andy’);
SE15: Objects and References
21–19
Objects as properties of Objects
References are used to
refer to one object from another
White Queen:Piece
Colour = white
name = q
inPlay = yes
Board
Position = a1
a1 =
White King:Piece
a2 =
Colour = white
name = k
inPlay = yes
Position = a2
SE15: Objects and References
21–20
Objects as properties of Objects
Person
name = Liz Bennet
age = 22
Person
mother =
name = Mrs Bennet
age = 22
Person
mother =
name = Ann Bennet
age = 22
mother =
SE15: Objects and References
21–21
Summary
Looked at how Java uses references to identify objects
Identified some potential pitfalls of this model
Looked at defining equals methods
Seen how arrays make use of references.
SE15: Objects and References
21–22
Follow-up Work
Savitch chapter 4
Read up on using Primitive and Class types as
parameters for methods
What is the biggest difference between a parameter of a
primitive type and a parameter of a class type?
SE15: Objects and References
21–23