Java Coding 5 - Bilkent University
Download
Report
Transcript Java Coding 5 - Bilkent University
Java Coding 5 – Part 2
To object or not…
David Davenport
Computer Eng. Dept.,
Bilkent University
Ankara - Turkey.
email: [email protected]
IMPORTANT…
Students…
This presentation is designed to be used in class as
part of a guided discovery sequence. It is not selfexplanatory! Please use it only for revision purposes
after having taken the class. Simply flicking through
the slides will teach you nothing. You must be actively
thinking, doing and questioning to learn!
Instructors…
You are free to use this presentation in your classes
and to make any modifications to it that you wish. All
I ask is an email saying where and when it is/was
used. I would also appreciate any suggestions you
may have for improving it.
thank you,
David.
Object vs. Reference
In the “real” world
david
{Person}
So too in
Java!
derya
{Person}
CS101 instructor
{Person}
Derya’s dad
{Person}
david2
{Person}
Object vs. Reference
In the Java world
David
david
{Person}
derya
{Person}
CS101 instructor
{Person}
Derya’s dad
{Person}
david2
{Person}
Derya
David’s
clone
Same or Different? (1)
Comparing objects
myQCd
{CD}
myCd
{CD}
Title
B.R.
Artist Queen
Date 1976
Length 3:50
yourCd
{CD}
if ( myCd == yourCd)
System.out.println( “Same”);
else
System.out.println( “Different”);
if ( myCd == yourQCd)
System.out.println( “Same”);
else
System.out.println( “Different”);
yourQCd
{CD}
Title
Best of
Artist Genesis
Date 1983
Length 2:40
Title
B.R.
Artist Queen
Date 1976
Length 3:50
Same or Different? (2)
Define an “equals” method
myQCd
{CD}
myCd
{CD}
Title
B.R.
Artist Queen
Date 1976
Length 3:50
yourCd
{CD}
if ( myCd.equals( yourCd) )
System.out.println( “Same”);
else
System.out.println( “Different”);
if ( myCd.equals( yourQCd) )
System.out.println( “Same”);
else
System.out.println( “Different”);
yourQCd
{CD}
Title
Best of
Artist Genesis
Date 1983
Length 2:40
Title
B.R.
Artist Queen
Date 1976
Length 3:50
Copying
in primitive vs. Object types…
int i, j;
i = 5;
j = i;
i++;
Sys… ( i, j);
Different
Person me, x;
me = new Person( …);
x = me;
me.setComments( “nice!”);
Sys… ( me.getComments()
+ x.getComments(), );
Same!
Copy vs. Clone
myCd
{CD}
Title
B.R.
Artist Queen
Date 1976
Length 3:50
yourQCd
{CD}
Title
B.R.
Artist Queen
Date 1976
Length 3:50
favouriteCd
{CD}
favouriteCd = myCd;
yourQCd = myCd.clone();
Parameter Passing (1)
Primitive types…
public int xyz( int i) {
i++;
return i;
}
main
int a, b;
a = 5;
b = xyz( a);
Sys… ( a, b);
main
a 5
b 6
xyz
5
6
i
Parameter Passing (2)
Object types…
public Person xyz( Person x) {
x.setComments(“Nice);
return x;
}
David
22
1000
“”
Nice
main
a
main
Person a, b;
a = new Person( “David” …);
b = xyz( a);
Sys… ( a.getComments()
+ b.getComments() );
b
xyz
x
Parameter Passing (3)
Object types…
public Person xyz( Person x) {
x = new Person( “Derya” …);
x.setComments(“Nice);
return x;
}
David
22
1000
“”
main
a
main
Person a, b;
a = new Person( “David” …);
b = xyz( a);
Sys… ( a.getComments()
+ b.getComments() );
b
Derya
18
500
“”
Nice
xyz
x
All Objects…
automatically have
boolean equals( Object)
Object clone()
String toString()
Code using
these
methods will
compile & run
even if your
class does not
define them!
BUT
they may not do what you would
like/expect, so implement yourself!
Lost objects & null
Java collects its garbage!
Title
B.R.
Artist Queen
Date 1976
Length 3:50
yourCd
{CD}
Title
Best of
Artist Genesis
Date 1983
Length 2:40
myCd
{CD}
aCd
{CD}
myCd = yourCd;
aCd = null;
Static vs. instance
Keep count of number of Person objects
Person
static
instance
David
22
2000
“Quiet”
a
b
count 1
0
4
3
2
name
age
salary
comments
Derya
18
500
“Nice”
c
Gunes
21
1500
“Sunny”
d
Ayse
25
1000
“Happy”