20
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: [email protected] To object or not…

Java Coding 5 – Part 2

  • Upload
    tiger

  • View
    43

  • Download
    0

Embed Size (px)

DESCRIPTION

Java Coding 5 – Part 2. To object or not…. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: [email protected]. IMPORTANT…. Students… - PowerPoint PPT Presentation

Citation preview

Page 1: Java Coding 5 – Part 2

Java Coding 5 – Part 2

David Davenport

Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.

email: [email protected]

To object or not…

Page 2: Java Coding 5 – Part 2

IMPORTANT… Students…

This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! 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.

Page 3: Java Coding 5 – Part 2

Object vs. Reference In the “real” world

david{Person}

CS101 instructor{Person}

derya{Person}

Derya’s dad{Person}

So too in Java!

david2{Person}

Page 4: Java Coding 5 – Part 2

Object vs. Reference In the Java world

need to revise our mental model!

David Deryadavid{Person}

CS101 instructor{Person}

derya{Person}

Derya’s dad{Person}

David’sclonedavid2

{Person}

Page 5: Java Coding 5 – Part 2

Same or Different? (1)

Comparing objects

Title B.R.Artist QueenDate 1976Length 3:50

myCd{CD}

Title B.R.Artist QueenDate 1976Length 3:50yourQCd

{CD}

Title Best ofArtist GenesisDate 1983Length 2:40yourCd

{CD}

if ( myCd == yourCd)System.out.println( “Same”);

elseSystem.out.println( “Different”);

if ( myCd == yourQCd)System.out.println( “Same”);

elseSystem.out.println( “Different”);

myQCd{CD}

Page 6: Java Coding 5 – Part 2

Same or Different? (2)

Define an “equals” method

Title B.R.Artist QueenDate 1976Length 3:50

myCd{CD}

Title B.R.Artist QueenDate 1976Length 3:50yourQCd

{CD}

Title Best ofArtist GenesisDate 1983Length 2:40yourCd

{CD}

if ( myCd.equals( yourCd) )System.out.println( “Same”);

elseSystem.out.println( “Different”);

if ( myCd.equals( yourQCd) )System.out.println( “Same”);

elseSystem.out.println( “Different”);

myQCd{CD}

Page 7: Java Coding 5 – Part 2

Copying in primitive vs. Object types…

int i, j;i = 5;j = i;i++;Sys… ( i, j);

Person me, x;me = new Person( …);x = me;me.setComments( “nice!”);Sys… ( me.getComments()

+ x.getComments(), );

DifferentSame!

Page 8: Java Coding 5 – Part 2

Copy vs. Clone

Title B.R.Artist QueenDate 1976Length 3:50

Title B.R.Artist QueenDate 1976Length 3:50

myCd{CD}

yourQCd{CD}

favouriteCd{CD}

favouriteCd = myCd; yourQCd = myCd.clone();

Page 9: Java Coding 5 – Part 2

Parameter Passing (1)

Primitive types…

public int xyz( int i) {i++;return i;

}

int a, b;a = 5;b = xyz( a);Sys… ( a, b);

5a

b

main

main

5 i

xyz

6

6

Page 10: Java Coding 5 – Part 2

Parameter Passing (2)

Object types…

public Person xyz( Person x) {x.setComments(“Nice);return x;

}

Person a, b;a = new Person( “David” …);b = xyz( a);Sys… ( a.getComments()

+ b.getComments() );

a

b

main

main

x

xyz

David22

1000“”Nice

Page 11: Java Coding 5 – Part 2

Parameter Passing (3)

Object types…

public Person xyz( Person x) {x = new Person( “Derya” …);x.setComments(“Nice);return x;

}

Person a, b;a = new Person( “David” …);b = xyz( a);Sys… ( a.getComments()

+ b.getComments() );

a

b

main

main

x

xyz

David22

1000“”

Derya18500“”Nice

Page 12: Java Coding 5 – Part 2

All Objects… automatically have

boolean equals( Object) Object clone() String toString()

BUT they may not do what you would

like/expect, so implement yourself!

Code using these methods will compile &

run even if your class does not

define them!

Page 13: Java Coding 5 – Part 2

Lost objects & null Java collects its garbage!

Title B.R.Artist QueenDate 1976Length 3:50

Title Best ofArtist GenesisDate 1983Length 2:40

myCd{CD}

yourCd{CD}

myCd = yourCd;

aCd{CD}

aCd = null;

Page 14: Java Coding 5 – Part 2

STATIC VS. INSTANCE

Page 15: Java Coding 5 – Part 2

Static vs. instance Variables “count” as an instance variable

-- each instance (object) has count variable

nameagesalarycomments

Person

instance

0David

222000

“Quiet”

0Derya

18500

“Nice”

0Gunes

211500

“Sunny”

0Ayse25

1000“Happy”

count

a b c d

23

c.count++;c.count++;a.count = 3;

Page 16: Java Coding 5 – Part 2

Static vs. instance Variables “count” as a static variable

-- only one count variable, associated with class

nameagesalarycomments

Person

static 01234

instance

David22

2000“Quiet”

Derya18

500“Nice”

Gunes21

1500“Sunny”

Ayse25

1000“Happy”

count

a b c d

initialise count to zero, then increment it

in constructor

also known as “class variables”

Page 17: Java Coding 5 – Part 2

Misc: Can combine, so static “nextID”

gives next value to be assigned to instance variable “personID”.

Constants often defined as statichence saving space

public static final int PI = 3.142;public static final String COMPANY = “Bilkent University”;

Page 18: Java Coding 5 – Part 2

Static vs. instance Methods Classes can have both

static & instance methods. Static methods useful when

accessing static variablespublic static int getCount()

object state is not neededpublic static int getAge( day, month, year)

Static methods cannot access instance variables or methods

Instance methodscan access static & instance, variables & methods

Page 19: Java Coding 5 – Part 2

Singletons (design pattern)

Problem: Ensure only a single instance of a class is created.(for database or network connections, etc.)

Solution: Combine static variable, private constructor & static method!

public class SingletonClass {

private static SingletonClass ourInstance = new SingletonClass();

private SingletonClass() { }

public static SingletonClass getInstance() { return singletonObj;

} }

Page 20: Java Coding 5 – Part 2