Oop lecture9 13

509 views
398 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
509
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture9 13

  1. 1. Lecture 13Iteration in Java Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. Simple c-style iterationpublic class IterationDemo { public static void main(String[] args) { List<String>lst = new ArrayList<String>(); lst.add("One"); lst.add("Two"); lst.add("Three"); for (int i=0; i<lst.size(); i++) System.out.println(lst.get(i)); }} Output: One Two Three
  3. 3. More iterators //more eye-friendly iteration for (String s : lst) System.out.println(s); Gives the same outputMost generic iteration—using Iterator Iterator<String>it = lst.iterator(); while (it.hasNext()) System.out.println(it.next()); Gives the same output
  4. 4. Iterating over a setSet<String>s = new HashSet<String>();s.add("One");s.add("Two");s.add("Three");Iterator<String>it = lst.iterator();while (it.hasNext()) System.out.println(it.next());Gives the same output:OneTwoThree
  5. 5. Iterating over a mapMap<String, String>map = new HashMap<String, String>();map.put("One", "111111");map.put("Two", "222222");map.put("Three", "333333");Iterator<Map.Entry<String, String>>it = map.entrySet().iterator();while (it.hasNext()){ Map.Entry<String, String>entry = it.next(); System.out.println(entry.getKey() + "--" + entry.getValue());} Output: Three--333333 One--111111 Two--222222
  6. 6. Remember old style iteration still works for arrays String[] str = new String[]{"One", "Two", "Three"}; for (int i=0; i<str.length; i++) System.out.println(str[i]); Output: One Two ThreeString[] str = new String[]{"One", "Two", "Three"};for (String s : str) System.out.println(s); Output: One Two Three
  7. 7. Some common methods present in all objectstoString()equals()hashCode()finalize()
  8. 8. toString()public class Student { int id; String name; public Student(int id, String name) { super(); this.id = id; this.name = name; } public String toString(){ return this.id + "--" + this.name; }}public static void main(String[] args){ Student student = new Student(3, "Joe"); System.out.println(student);}Output:3--Joe
  9. 9. Another toString() demoList<Student>stus = new ArrayList<Student>();Student st = new Student(1, "John");stus.add(st);stus.add(st);System.out.println("Printing list: ");for (Student s: stus) System.out.println(s); Output: Printing list: 1--John 1--John
  10. 10. hashCode() demo with equals()public class HashCodeDemo { public static class Student { int id; String name; public Student(int id, String name) { super(); this.id = id; this.name = name; } public String toString() { return this.id + "--" + this.name; } /* public boolean equals(Object obj) { Student arg = (Student) obj; return this.id == arg.id; }*/ public int hashCode() { return this.id; }}
  11. 11. public static void main(String[] args) { Map<Student, String> map = new HashMap<Student, String>(); map.put(new Student(1, "John"), null); map.put(new Student(1, "John"), null); map.put(new Student(2, "John"), null); System.out.println(map.size()); Iterator<Map.Entry<Student, String>> it1 = map.entrySet().iterator(); System.out.println("Printing map: "); while (it1.hasNext()) System.out.println(it1.next()); }} Output: Now we uncomment the equals() method in the 3 previous slide: Printing map: 1--John=null Output: 1--John=null 2 2--John=null Printing map: 1--John=null 2--John=null Explanation: hashCode merely specifies a slot; equals() helps put the object in the slot
  12. 12. equals() method Two objects are equal if their equals() method returns true. Demo:public class Student { int id; String name; public Student(int id, String name) { super(); this.id = id; this.name = name; } public String toString(){ return this.id + "--" + this.name; } public int hashCode() { return this.id; } public boolean equals(Object obj) { Student s = (Student)obj; if (s.id == this.id) return true; return false; }
  13. 13. equals() continued System.out.println(st.equals(st2)); System.out.println(st==st2);Output:truefalseIn java “==” operator is not same as “equals()”
  14. 14. finalize() What is garbage collection? In C/C++ the programmer can get a chunk of program using malloc() and can dispose memory using memfree() Having programmer free will at memory management resulted in memory leaks in many C programs Java will not let programmer directly acquiring memory. Rather JVM manages memory
  15. 15. Finalize() (c..)• When an object is de-referenced, the object is automatically removed from the memory by JVM.• Whenever, an object is removed its finalize() method is called• Garbage collection is automatically scheduled by the JVM• However, a programmer can trigger a garbage collection by calling System.gc()• Example in the next page:
  16. 16. Finalize() (c..)public class AnObject { protected void finalize() throws Throwable { super.finalize(); System.out.println("Finalize method is called"); } public static void main(String[] args){ new AnObject(); }} Output:
  17. 17. Finalize() (c..)public class AnObject { protected void finalize() throws Throwable { super.finalize(); System.out.println("Finalize method is called"); } public static void main(String[] args){ new AnObject(); System.gc(); }}Output:Finalize method is called
  18. 18. Garbage collection formally definedGarbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection

×