1 / 20

Java Coding 5 – Part 2

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…

tiger
Download Presentation

Java Coding 5 – Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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.

  3. david{Person} derya{Person} CS101 instructor{Person} Derya’s dad{Person} david2{Person} Object vs. Reference • In the “real” world So too in Java!

  4. 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

  5. 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”);

  6. 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”);

  7. 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!

  8. 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();

  9. 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);

  10. 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() );

  11. 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() );

  12. 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!

  13. 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;

  14. Static vs. Instance

  15. 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

  16. 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

  17. 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”;

  18. 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

  19. 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; } }

More Related