23-Java5-Features

23-Java5-Features - 2006 Marty Hall New Features in Java 5:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
2 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall New Features in Java 5: A Quick Summary 3 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 J2EE training: http://courses.coreservlets.com Agenda The for/each loop Scanner and command-line input Generics Autoboxing Varargs Printf Reading command-line input 5 J2EE training: http://courses.coreservlets.com 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 item: collectionOrArrayOfTypes) { doSomethingWith(item); } Applies to List, Map, Queue, Set, and other collections Arrays
Background image of page 2
6 J2EE training: http://courses.coreservlets.com 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 7 J2EE training: http://courses.coreservlets.com Reading Simple Input: the Scanner Class Basic Syntax Attach a Scanner to System.in Call nextLine, nextInt, nextDouble, etc Examples 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 J2EE training: http://courses.coreservlets.com Example: Printing Random Numbers import java.util.*; public class RandomNums { public static void main(String[] args) { System.out.print("How many random nums? "); Scanner inputScanner = new Scanner(System.in); int n = inputScanner.nextInt(); for(int i=0; i<n; i++) { System.out.println("Random num " + i + " is " + Math.random()); } } } How many random nums? 5 Random num 0 is 0.22686369670835704 Random num 1 is 0.0783768527137797 Random num 2 is 0.17918121951887145 Random num 3 is 0.3441924454634313 Random num 4 is 0.6131053203170818 9 J2EE training: http://courses.coreservlets.com Using Generics Find a data structure that accepts Object(s) ArrayList, LinkedList, HashMap, HashSet, Stack Not arrays: they already take explicit types! Declare the data structure with the type(s) in
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

23-Java5-Features - 2006 Marty Hall New Features in Java 5:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online