oop lecture9 13

18
Lecture 13 Iteration in Java Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Upload: shahriar-robbani

Post on 11-Nov-2014

542 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Oop lecture9 13

Lecture 13

Iteration in Java

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture9 13

Simple c-style iteration

public 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: OneTwoThree

Page 3: Oop lecture9 13

More iterators//more eye-friendly iterationfor (String s : lst) System.out.println(s);

Gives the same output

Iterator<String>it = lst.iterator();while (it.hasNext()) System.out.println(it.next());

Gives the same output

Most generic iteration—using Iterator

Page 4: Oop lecture9 13

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

Page 5: Oop lecture9 13

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--333333One--111111Two--222222

Page 6: Oop lecture9 13

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]);

String[] str = new String[]{"One", "Two", "Three"};

for (String s : str) System.out.println(s);

Output:OneTwoThree

Output:OneTwoThree

Page 7: Oop lecture9 13

Some common methods present in all objects

toString()equals()hashCode()finalize()

Page 8: Oop lecture9 13

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

Page 9: Oop lecture9 13

Another toString() demo

List<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--John1--John

Page 10: Oop lecture9 13

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;

}}

Page 11: Oop lecture9 13

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:3Printing map: 1--John=null1--John=null2--John=null

Now we uncomment the equals() method in the previous slide:

Output:2Printing map: 1--John=null2--John=null

Explanation: hashCode merely specifies a slot; equals() helps put the object in the slot

Page 12: Oop lecture9 13

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;

}}

Page 13: Oop lecture9 13

equals() continued

System.out.println(st.equals(st2));System.out.println(st==st2);

Output:

truefalse

In java “==” operator is not same as “equals()”

Page 14: Oop lecture9 13

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

Page 15: Oop lecture9 13

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:

Page 16: Oop lecture9 13

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:

Page 17: Oop lecture9 13

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

Page 18: Oop lecture9 13

Garbage collection formally defined

Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection