Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 1.5 The New Java Mike Orsega Central Carolina CC.

Similar presentations


Presentation on theme: "Java 1.5 The New Java Mike Orsega Central Carolina CC."— Presentation transcript:

1 Java 1.5 The New Java Mike Orsega Central Carolina CC

2 Class Scanner Enumerated Types Autoboxing Variable Length Parameter Lists Generic Types New for Loop New Features in Java 1.5

3 Located in the java.util package Provides methods for:  Input  Parsing The Scanner Class

4 Input (the old way) import java.io.*; public class Hello { public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String name; System.out.print("Enter your name: "); name = br.readLine(); System.out.println("Hello " + name); } Import java.io.* Throws a checked IOException Create a BufferedReader with an InputStreamReader using System.in

5 Input (Java 1.5) import java.util.Scanner; public class Hello { public static void main (String [] args) { Scanner in = new Scanner(System.in); String name; System.out.print("Enter your name: "); name = in.nextLine(); System.out.println("Hello " + name); } Import java.util.Scanner No checked Exception Create a Scanner object only

6 Scanner is an iterator (collection of items) String nextLine()  Returns the remaining keyboard input line as a String. Note: ‘\n’ is read and discarded. boolean nextBoolean() double nextDouble() int nextInt() Throws an InputMismatchException if the next token isn’t of the correct type. This Exception is NOT checked Scanner Class

7 useDelimiter(String)  Makes the String the only delimiter used to separate input. next()  Returns input up to, but not including, the first delimiter (any whitespace character is the default) More Scanner Class

8 import java.util.Scanner; public class Age { public static void main(String [] args) { Scanner in = new Scanner(System.in); int age, age2; System.out.print("Enter your age: "); age = in.nextInt(); age2 = in.nextInt(); System.out.println("Boy " + age + " is old!"); System.out.println(age2 + " is even worse!"); } OUTPUT Enter your age: 10 12 Boy 10 is old! 12 is even worse! Examples OUTPUT

9 import java.util.Scanner; public class Delimit { public static void main(String [] args) { Scanner in = new Scanner(System.in); String word1, word2; in.useDelimiter("#"); System.out.println("Enter two words: "); word1 = in.next(); word2 = in.next(); System.out.println(word1 + " was entered first"); System.out.println("Then you entered " + word2); } OUTPUT Enter two words: one#two# one was entered first Then you entered two OUTPUT Enter two words: one two# three# one two was entered first Then you entered three More Examples OUTPUT

10 An enumerated type creates a new type of variable and establishes all possible values by listing them. enum Grade { A, B, C, D, F}; Grade myGrade = Grade.A; if (myGrade == Grade.F) System.out.println(“You fail”); System.out.println(myGrade); Enumerated Types

11 No invalid value can be assigned Internally, each value has a corresponding ordinal value, starting at 0 You cannot use the ordinal values directly. Instead you can use the ordinal() method Java also provides a name() method enum types cannot be declared locally You can pass enum variables Enumerated Types

12 public class Enumerated { enum Grade { A, B, C, D, F}; public static void main(String [] args) { Grade myGrade = Grade.A; if (myGrade == Grade.F) System.out.println("You fail"); else System.out.println("You pass"); System.out.println("myGrade: " + myGrade); System.out.println("myGrade.ordinal(): " + myGrade.ordinal()); System.out.println("myGrade.name(): " + myGrade.name()); } Enumerated Example

13 Autoboxing is the automatic conversion from a primitive value to the corresponding wrapper object Integer myInt; int number = 5; myInt = number; Unboxing is the conversion from the object to the primitive type Integer myInt2 = new Integer(15); int number; number = myInt2; Autoboxing

14 Java methods can now accept a variable number of arguments These arguments will be stored in an array public static void main(String [] args) { myMethod("A", "B", "C", "D"); } public static void myMethod(String... data) { for (int i=0; i<data.length;i++) System.out.println(data[i]); } OUTPUT A B C D Variable Length Arguments OUTPUT

15 Methods can accept other parameters, in addition to the varargs The variable length parameter must come last in the argument list Can only accept one variable length paramter void method2 (int a, double b, int … numbers) Variable Length Arguments

16 Classes and methods can have a type parameter Any class type can be plugged in The class definition is compiled just like any other class Once compiled, it can be used like any other class, except that the code must specify a class type to replace the parameter Generic Types

17 public class Generic { private T item; public void setItem(T _item) { item = _item; } public T getItem() { return item; } } Generics Example Placeholder for data type

18 public class TestGeneric { public static void main(String [] args) { Generic myGen = new Generic (); myGen.setItem("Hello!"); System.out.println("Item is: " + myGen.getItem()); Generic intGen = new Generic (); intGen.setItem(5); System.out.println("Item is: " + intGen.getItem()); } } OUTPUT Item is: Hello! Item is: 5 Generics Example (cont) Class type here OUTPUT

19 A new version of the for loop has been added to make going through an iterator easier: If you had a NameList iterator that stores Name objects, we could use: for (Name myName : NameList) System.out.println(myName); This is equivalent to: Name myName; while (NameList.hasNext()) { myName = NameList.next(); System.out.println(myName); } Declare variable Iterator object The New for Loop

20 This works with array variables as well int [] list = {1, 2, 3, 4, 5}; for (int num : list) System.out.println(num); OUTPUT 1 2 3 4 5 The New for Loop OUTPUT

21 Questions


Download ppt "Java 1.5 The New Java Mike Orsega Central Carolina CC."

Similar presentations


Ads by Google