java 5.0 java 5 – tiger short history. where ap is headed: likely features that will end up in the...

17
Java 5.0 Java 5 – Tiger • Short history. • Where AP is headed: likely features that will end up in the subset. • Other fun features to use if you choose

Upload: asher-rich

Post on 16-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Java 5 – Tiger

• Short history.

• Where AP is headed: likely features that will end up in the subset.

• Other fun features to use if you choose

Page 2: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Short History

• Java 1.1 to 1.2 had some language changes

• From 1.2 until Java 5, essentially no language feature changes – just additions to the APIs

• Java 5 – Tiger (originally called 1.5 during beta) now has many additional language features

• AP will adopt a couple

Page 3: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Generics• Like “templates” in C++• Pedagogically, this is more sound. Let’s declare to

the compiler upfront what we’re going to put in – if a student doesn’t, she’ll get a compiler error. The old way, the runtime environment would discover that someone didn’t put the right thing in/out and throw a run-time exception – less desirable – harder for students. Also, this is now self-documenting.

ArrayList<Fish> list = new ArrayList<Fish>();

list.add( new Fish(…) );Fish f = list.get(0);

Realize that one could put a Fish or any subclass of Fish into this list. One could also choose to put an interface or abstract class type within the <>

Page 4: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Auto-Boxing/Unboxing

• Automatically put primitives into Wrapper classes – and go other way as well. Works for all 8 primitive types and their respective Wrapper class.

Integer intObj = 1;int x = intObj;

Yes! Nice!

int answer = intObj + x;

Page 5: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0More generic Generics examples

(sorry)

Let’s keep track of how many of each color crayon we have…

Map<String, Integer> crayons = new TreeMap<String, Integer>();

crayons.put(“red”, 1);crayons.put(“blue”, 2);…int count = crayons.get("blue");count++;crayons.put("blue", count);

Page 6: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Your Turn!Declare a structure that would allow you to keep a list of

teacher names (we’d eventually like to print them in alpha) who have a particular color crayon(we’d like the crayon colors to also come out in alpha order)

Red crayon Don, Sharon, Joe, Mike

Blue crayon Kent, Doug, Dan, Carol

Green crayon Harlie, Jennifer, Gerard

Yellow crayon Mark, Sam, Kevin, Norm

Orange crayon Shirley, Regina, Dave, Jon

(you have 10 minutes – go try – get all the names in and then print)

Page 7: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Here’s one way Map<String, Set<String>> crayons =

new TreeMap<String, Set<String>>(); Set s = new TreeSet(); s.add("Don"); s.add("Sharon"); s.add("Joe"); s.add("Mike"); crayons.put("red",s);

s = new TreeSet(); s.add("Kent"); s.add("Doug"); s.add("Dan"); s.add("Carol"); crayons.put("blue",s); System.out.println(crayons);

Page 8: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

What will AP do?

• So, AP will probably adopt Generics and Auto-boxing/unboxing for the ’07 exam– No official word yet – but it’s almost for sure

• May or may not adopt the “For each” loop– Leads to cleaner questions – so probably

Page 9: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

“For Each” loop

Fish[] fishes = …;

for (Fish f : fishes)

f.act();

Pros: no OBOB’s – cleaner

Cons: can’t skip 1st, last, increment some index, delete an element

Page 10: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

more For Each…

ArrayList<Fish> fishList = …;

for (Fish f : fishList)

f.act();

Page 11: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

more For Each…

int[] nums = {1,2,3,4,5};

for (int x : nums)

System.out.println(x);

Page 12: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

more For Each…

public static void setsAreIterable() {

Set<Integer> s = new TreeSet<Integer>();

s.add(1); s.add(2); s.add(3); for (int x : s) System.out.println( x );}

Page 13: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

more For Each…

• In general, any class that implements Iterable will allow you to use the “For Each” loop

Page 14: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

AP Probably Won’t adopt…

• Scanner

• Variable number of parameters

Page 15: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Scanner// from console

Scanner in = new Scanner(System.in);

System.out.print("How old are you?");

int age = in.nextInt();

// from file

String fileName = . . .;

Scanner file = new Scanner(new File(fileName));

Easy – no more third-party packages necessary

Page 16: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

Variable number of parameters

public static double average(double... values)

{

assert values.length > 0;

double sum = 0;

for (double v : values)

sum += v;

return sum / values.length;

}

Page 17: Java 5.0 Java 5 – Tiger Short history. Where AP is headed: likely features that will end up in the subset. Other fun features to use if you choose

Java

5.0

For more…

• See apcentral.collegeboard.com