Download presentation
Presentation is loading. Please wait.
Published byVivien Bruce Modified over 8 years ago
1
Object Oriented Programming in Java Lecture 14
2
Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print the return values.
3
Java 5 new Features Ease of development Quality monitoring and manageability Performance and scalability
4
Generics Allow to pass types as arguments to classes just as values to methods Why ? - compile time safety - no need in casts C++: templates are handled on macro processor level
5
Examples public class LinkedList { boolean add(Element o) { // code omitted } Element getFirst(){ // code omitted }
6
Examples public class Ex2 { private void testCollection() { List list = new ArrayList (); list.add(new String("Hello world!")); list.add(new String("Good bye!")); list.add(""+new Integer(66)); printCollection(list); }
7
private void printCollection(Collection c) { Iterator i = c.iterator(); while(i.hasNext()) { String item = i.next(); System.out.println("Item: "+item); } public static void main(String argv[]) { Ex2 e = new Ex2(); e.testCollection(); } Examples
8
Advantages Allow to pass types as arguments to classes just as values to methods Why ? - compile time safety - no need in casts
9
Enhanced loop Syntax for (FormalParameter : Expression) Statement Note: Expression must be an array or an instance of a new interface called java.lang.Iterable
10
public void oldFor(Collection c) { for(Iterator i = c.iterator(); i.hasNtext(); ) { String str = (String) i.next(); System.out.println(str); } public void newFor(Collection c) { for(String str : c) { System.out.println(str); }
11
public int sumArray(int array[]) { int sum = 0; for(int i=0;i<array.length;i++) sum += array[i]; return sum; }-------------------------------------------------- public int sumArray(int array[]) { int sum = 0; for(int i : array) sum += i; return sum; } Loops for arrays
12
Static imports Enable to import methods and fields from the static members of the class or interface, without qualifying the name import static java.lang.System.out; ……….. out.print("stuff"); out.print("more stuff");
13
Var-args Multiple arguments can be passed to the routines: public static void myMethod(Object... args){ for(Object a:args) System.out.print(""+a+" "); System.out.println(); } … myMethod(23, 34, 78,"Hello", "Goodbye"); System.out.printf("%d %d %d %s %s",23, 34, 78,"Hello", "Goodbye");
14
Input/Output Output System.out.printf("%s %3d", name, age); Input Scanner reader = new Scanner(System.in); int n = reader.nextInt();
15
Enumerations Example: enum Drinks {pepsi, coke, schweppes }; …. Drinks d = Drinks.coke; System.out.print(d); …. Note: In C++ enumerations are just hidden integers. In Java enumerations are generated classes.
16
Example public enum Shapes{ ins1(1,2), ins2(1,3) ; private int x; private int y; Shapes(int x, int y){ this.x = x; this.y = y; } ….. Shapes s = Shapes.ins1; Shapes s2 Shapes.ins2;
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.