• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oop lecture9 13
 

Oop lecture9 13

on

  • 441 views

 

Statistics

Views

Total Views
441
Views on SlideShare
441
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Oop lecture9 13 Oop lecture9 13 Presentation Transcript

    • Lecture 13Iteration in Java Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
    • 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
    • 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
    • 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
    • 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
    • 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
    • Some common methods present in all objectstoString()equals()hashCode()finalize()
    • 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
    • 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
    • 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; }}
    • 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
    • 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; }
    • equals() continued System.out.println(st.equals(st2)); System.out.println(st==st2);Output:truefalseIn java “==” operator is not same as “equals()”
    • 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
    • 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:
    • 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:
    • 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
    • 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