Date post: | 20-Dec-2015 |
Category: |
Documents |
View: | 222 times |
Download: | 0 times |
SE15: Objects and References 21–2
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–3
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–4
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’;
10
a
score
symbolTwo memory Locations for thetwo variables(known size)
SE15: Objects and References 21–5
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
myBook2Two memory Locations for thetwo variables
:
SE15: Objects and References 21–6
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–7
References to objects
Book myBook1 = new Book();
Book myBook2 = new Book();
3000
1052
myBook1
myBook2
??
??
1052
3000
Two memory Locations for thetwo objects(could be any value)
:
SE15: Objects and References 21–8
References to objects
myBook1.setTitle(‘Macbeth’);
myBook1.setAuthor(‘Shakespeare’);
myBook2.setTitle(‘Programming’);
myBook2.setAuthor(‘Pickle’);
3000
1052
myBook1
myBook2
ProgrammingPickle
MacbethShakespeare
1052
3000
:
SE15: Objects and References 21–9
Assignment (=) with primitive types
int score1 = 10;
int score2 = 34;
score2 = score1;
10
34
score1
score2
:
10
10
score1
score2
:
SE15: Objects and References 21–10
Assignment (=) with Class variables
myBook2 = myBook1;
3000
3000
myBook1
myBook2
ProgrammingPickle
MacbethShakespeare
1052
3000
:
SE15: Objects and References 21–11
myBook2.setTitle(‘Java’);
myBook2.setAuthor(‘Savitch’);
myBook1 and myBook2 are now
two names for the same object!
3000
3000
myBook1
myBook2
ProgrammingPickle
JavaSavtich
1052
3000
:
SE15: Objects and References 21–12
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–13
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”);
elseSystem.out.println(“They are NOT equal”);
This will produce the outputThey are NOT equal
SE15: Objects and References 21–14
Why are they NOT equal?
myBook1.setTitle(‘Java’);
myBook1.setAuthor(‘Savitch’);
myBook2.setTitle(‘Java’);
myBook2.setAuthor(‘Savitch’);
Because 1052 is not equal to 3000
(The == operator only checks if the
memory addresses are the same)
3000
1052
myBook1
myBook2
JavaSavitch
JavaSavitch
1052
3000
:
SE15: Objects and References 21–15
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–16
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;
elsereturn false;
}}
SE15: Objects and References 21–17
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–18
Arrays and references
2678
2426
libraryBook[0]
JavaSavitch
MacbethShakespeare
2380
2426
:
libraryBook[1]
2380libraryBook[2]
ProgrammingPickle
2678
?
?
libraryBook[0]
:
libraryBook[1]
?libraryBook[2]
?
?
?
libraryBook[3]
libraryBook[4]
libraryBook[5]
libraryBook[6] ?
SE15: Objects and References 21–19
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–20
Objects as properties of Objects
References are used to
refer to one object from another
Board
a1 =
a2 =
White Queen:Piece
Colour = white
name = q
inPlay = yes
Position = a1
White King:Piece
Colour = white
name = k
inPlay = yes
Position = a2
SE15: Objects and References 21–21
Objects as properties of Objects
Person
name = Ann Bennet
age = 22
mother =
Person
name = Liz Bennet
age = 22
mother =
Person
name = Mrs Bennet
age = 22
mother =
SE15: Objects and References 21–22
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.