Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0.

Similar presentations


Presentation on theme: "Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0."— Presentation transcript:

1 Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0

2 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Typed collections (Generics) Enhanced For Loop Autoboxing and Unboxing Typesafe Enums Static Imports Input using Scanner

3 3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed collections (Generics) Collections use polymorphism to store objects of any type. A drawback is type loss on retrieval: Lot selected = (Lot)lots.get(i); Use of untyped collections is considered ‘unsafe’. Typed collections avoid type loss. Runtime checks are simplified because the type is known.

4 4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling New syntax The type of object to be stored is indicated on declaration: private ArrayList notes;... and on creation: notes = new ArrayList (); Collection types are parameterized.

5 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using a typed collection Because the collection is typed, no cast is required on retrieval: Lot selection = lots.get(i);

6 6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed Iteration A typed iterator can be obtained from a typed collection: Iterator it = lots.iterator(); while(it.hasNext()) { Lot lot = it.next(); System.out.println(lot); }

7 7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed HashMaps HashMaps operate with (key,value) pairs. A typed HashMap required two type parameters: private HashMap responses;... responses = new HashMap ();

8 8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Enhanced For Loop Iteration over arrays and collections is a common operation. It is easy to get the terminating condition wrong. An enhanced for loop simplifies iteration. –Only if the loop variable is not required.

9 9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Enhanced For Loop for(int i = 0; i < array.length; i++) { Type element = array[i];... } for(Type element : array) {... } Original style Enhanced style ‘for each element in array’

10 10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling For loop with Iterator Iterator it = entries.iterator(); while(it.hasNext()) { System.out.println(it.next()); } Original style for(LogEntry entry : entries) { System.out.println(entry); } Enhanced style

11 11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Autoboxing and Unboxing Wrapper classes not required when using primitive types in object contexts. Autoboxing: int i = 18; myCollection.add(i); Unboxing : int i = myCollection.get(0);

12 12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typesafe Enums A new syntax allowing definition of enumerated types. Will typically replace lists of named, related, integer values, such as: public static final int LOW = 0, MEDIUM = 1, HIGH = 2;

13 13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling An Enumerated Type Values are referred to like static variables, e.g. Level.LOW Values are distinct from ints and have no associated numeric value. public enum Level { LOW, MEDIUM, HIGH }

14 14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Static Imports Allow import of static methods and variables, e.g. import static java.lang.Math.abs; import static java.lang.Math.PI; Imported names can be used without qualification. A relatively minor addition to the language.

15 15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Input using Scanner Defined in the java.util package. Provides a way to read and process input. Substitutes for existing combinations of classes, e.g. BufferedReader with StringTokenizer or String.split. Parses input and returns typed values.

16 16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Example using Scanner // Scan a line of text for ints. Scanner tokenizer = new Scanner(line); for(int i = 0; i < dataline.length; i++) { dataline[i] = tokenizer.nextInt(); }

17 17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review A range of new language features and classes are available in Java 5. Typed collections (generics) are probably the most important. The enhanced for loop is useful. Enumerated types improve clarity and type security of code.


Download ppt "Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0."

Similar presentations


Ads by Google