200 likes | 315 Views
Java Coding 5 – Part 2. To object or not…. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…
E N D
Java Coding 5 – Part 2 To object or not… David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr
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.
david{Person} derya{Person} CS101 instructor{Person} Derya’s dad{Person} david2{Person} Object vs. Reference • In the “real” world So too in Java!
david{Person} david2{Person} derya{Person} CS101 instructor{Person} Derya’s dad{Person} David’sclone Object vs. Reference • In the Java world • need to revise our mental model! David Derya
myQCd{CD} Title B.R. Artist Queen Date 1976 Length 3:50 Title Best of Artist Genesis Date 1983 Length 2:40 myCd{CD} yourCd{CD} Title B.R. Artist Queen Date 1976 Length 3:50 yourQCd{CD} Same or Different? (1) • Comparing objects if ( myCd == yourCd) System.out.println( “Same”); else System.out.println( “Different”); if ( myCd == yourQCd) System.out.println( “Same”); else System.out.println( “Different”);
myQCd{CD} Title B.R. Artist Queen Date 1976 Length 3:50 Title Best of Artist Genesis Date 1983 Length 2:40 myCd{CD} yourCd{CD} Title B.R. Artist Queen Date 1976 Length 3:50 yourQCd{CD} Same or Different? (2) • Define an “equals” method 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”);
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(), ); Different Same!
myCd{CD} yourQCd{CD} favouriteCd{CD} Copy vs. Clone Title B.R. Artist Queen Date 1976 Length 3:50 Title B.R. Artist Queen Date 1976 Length 3:50 favouriteCd = myCd; yourQCd = myCd.clone();
main xyz a i b Parameter Passing (1) • Primitive types… public int xyz( int i) { i++; return i; } 5 5 6 6 main int a, b; a = 5; b = xyz( a); Sys… ( a, b);
main xyz a x b Parameter Passing (2) • Object types… David221000“” public Person xyz( Person x) { x.setComments(“Nice); return x; } Nice main Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a.getComments() + b.getComments() );
main xyz a x b Parameter Passing (3) • Object types… David221000“” Derya18500“” public Person xyz( Person x) { x = new Person( “Derya” …); x.setComments(“Nice); return x; } Nice main Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a.getComments() + b.getComments() );
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!
aCd{CD} aCd = null; Lost objects & null • Java collects its garbage! Title B.R. Artist Queen Date 1976 Length 3:50 Title Best of Artist Genesis Date 1983 Length 2:40 yourCd{CD} myCd{CD} myCd = yourCd;
Person nameagesalarycomments instance 0 David222000“Quiet” 0 Derya18500“Nice” 0 Gunes211500“Sunny” 0 Ayse251000“Happy” Static vs. instance Variables • “count” as an instance variable-- each instance (object) has count variable c.count++; c.count++; a.count = 3; count 2 3 a b c d
Person static nameagesalarycomments instance David222000“Quiet” Derya18500“Nice” Gunes211500“Sunny” Ayse251000“Happy” Static vs. instance Variables • “count” as a static variable-- only one count variable, associated with class also known as “class variables” count 0 4 3 1 2 initialise count to zero, then increment itin constructor a b c d
Misc: • Can combine, so static “nextID” gives next value to be assigned to instance variable “personID”. • Constants often defined as static hence saving space public static final int PI = 3.142; public static final String COMPANY = “Bilkent University”;
Static vs. instance Methods • Classes can have both static & instance methods. • Static methods useful when • accessing static variables public static int getCount() • object state is not needed public static int getAge( day, month, year) • Static methods cannot access instance variables or methods • Instance methods can access static & instance, variables & methods
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; } }