A Quick Summary. For live Java training, please see training courses at

© 2009 Marty Hall New Features in Java 5: A Quick Summary Customized Java EE Training: http://courses.coreservlets.com/ 2 Servlets, JSP, JSF 1.x & J...
Author: Piers Wilkerson
1 downloads 0 Views 161KB Size
© 2009 Marty Hall

New Features in Java 5: A Quick Summary Customized Java EE Training: http://courses.coreservlets.com/ 2

Servlets, JSP, JSF 1.x & JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.

© 2009 Marty Hall

For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts Classic, Struts 2, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo, Prototype, Ext, etc.), GWT, Java 5, Java 6, Spring, Hibernate/JPA and customized combinations of topics Hibernate/JPA, topics. Taught by the author of Core Servlets and JSP, More Servlets and JSP, JSP and this tutorial. tutorial Available at public venues,Customized or customized Java EE Training: versions http://courses.coreservlets.com/ can be held on-site at your Servlets, JSP, JSF 1.x & JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. organization. Contact [email protected] for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Agenda • • • • • • •

The for/each loop Scanner and command-line input Generics Autoboxing Varargs Printf Reading command-line input

4

For/Each Loops • Idea – Instead of looping with a numeric index, set a local variable to each item of a collection or array in order

• Basic syntax – for(Type ( yp item: collectionOrArrayOfTypes) O yO yp ) { doSomethingWith(item); }

• Applies to – List, List Map, Map Queue, Queue Set, Set and other collections – Arrays 5

For/Each Loops: Example public static void listEntries(String[] entries) { for(String entry: entries) { System.out.println(entry); } }

• Result String[] test = {"This", "is", "a", "test"}; listEntries(test); This is a test 6

Reading Simple Input: the Scanner Class • Basic Syntax – Attach Att h a Scanner S to t System.in S t i – Call nextLine, nextInt, nextDouble, etc

• Examples p Scanner inputScanner = new Scanner(System.in); String s = inputScanner.nextLine(); int i = inputScanner.nextInt(); double d = inputScanner.nextDouble();

• Notes – Scanner for System.in is useful only for simple testing • Use a GUI in a real application

– But you can also attach a Scanner to a File, a Socket, a Process, and more 7

Example: Printing Random Numbers import java.util.*;

8

public class RandomNums { public static void main(String[] args) { System.out.print("How many random nums? "); S Scanner i inputScanner tS = new S Scanner(System.in); (S t i ) int n = inputScanner.nextInt(); for(int i=0; i