Java Coding 5 Part 2 To object or

Java Coding 5 – Part 2 To object or not… David Davenport Computer Eng. Dept. , Bilkent University Ankara - Turkey. email: david@bilkent. edu. tr

IMPORTANT… n 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! n 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 n In the “real” world david {Person} So too in Java! derya {Person} CS 101 instructor {Person} Derya’s dad {Person} david 2 {Person}

Object vs. Reference n In the Java world David david {Person} derya {Person} CS 101 instructor {Person} Derya’s dad {Person} david 2 {Person} Derya David’s clone

Same or Different? (1) n Comparing objects my. QCd {CD} my. Cd {CD} Title B. R. Artist Queen Date 1976 Length 3: 50 your. Cd {CD} if ( my. Cd == your. Cd) System. out. println( “Same”); else System. out. println( “Different”); if ( my. Cd == your. QCd) System. out. println( “Same”); else System. out. println( “Different”); your. QCd {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) n Define an “equals” method my. QCd {CD} my. Cd {CD} Title B. R. Artist Queen Date 1976 Length 3: 50 your. Cd {CD} if ( my. Cd. equals( your. Cd) ) System. out. println( “Same”); else System. out. println( “Different”); if ( my. Cd. equals( your. QCd) ) System. out. println( “Same”); else System. out. println( “Different”); your. QCd {CD} Title Best of Artist Genesis Date 1983 Length 2: 40 Title B. R. Artist Queen Date 1976 Length 3: 50

Copying n 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. set. Comments( “nice!”); Sys… ( me. get. Comments() + x. get. Comments(), ); Same!

Copy vs. Clone my. Cd {CD} Title B. R. Artist Queen Date 1976 Length 3: 50 your. QCd {CD} Title B. R. Artist Queen Date 1976 Length 3: 50 favourite. Cd {CD} favourite. Cd = my. Cd; your. QCd = my. Cd. clone();

Parameter Passing (1) n 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) n Object types… public Person xyz( Person x) { x. set. Comments(“Nice); return x; } David 22 1000 “” Nice main a main Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a. get. Comments() + b. get. Comments() ); b xyz x

Parameter Passing (3) n Object types… public Person xyz( Person x) { x = new Person( “Derya” …); x. set. Comments(“Nice); return x; } David 22 1000 “” main a main Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a. get. Comments() + b. get. Comments() ); b Derya 18 500 “” Nice xyz x

All Objects… n automatically have boolean equals( Object) n Object clone() n String to. String() n n Code using these methods will compile & run even if your class does not define them! BUT n they may not do what you would like/expect, so implement yourself!

Lost objects & null n Java collects its garbage! Title B. R. Artist Queen Date 1976 Length 3: 50 your. Cd {CD} Title Best of Artist Genesis Date 1983 Length 2: 40 my. Cd {CD} a. Cd {CD} my. Cd = your. Cd; a. Cd = null;

Static vs. instance n 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”

- Slides: 15