What`sNewInJava5 - Coweb - Georgia Institute of Technology

Download Report

Transcript What`sNewInJava5 - Coweb - Georgia Institute of Technology

What is new in Java 5.0 (1.5)?
Barb Ericson
Georgia Institute of Technology
June 2006
Georgia Institute of Technology
Learning Goals
• Understand the new features in Java 5.0
–
–
–
–
–
For-each loop
Generics
Automatic Boxing and Unboxing
New collection interfaces
Not covered on exam
• Enumerated Types
• Static Imports
• Formatted Input and Output
– For more information see
http://apcentral.collegeboard.com/members/article/1,3
046,151-165-0-49154,00.html
Georgia Institute of Technology
For Each Loop
• Used to loop through all items of a collection
(Array, List, Map, Set, etc)
– Syntax
for (Type varName : collectionName)
– Example
for (String name : nameList)
– Each time through the loop the varName will refer to a
different item in the collection until all the items in the
collection have been processed
– Use whenever you want to process each item in a
collection
• But not when you might want to remove an item from the
collection in the loop
Georgia Institute of Technology
Before the For-Each Loop
/**
* Method to set all the sample values to
* the maximum positive
* value if they were positive (including 0)
* and the minimum
* negative value if they were negative.
*/
public void forceToExtremes()
{
/* if the value was >= 0 set to the
* maximum positive value
*/
if (sample.getValue() >= 0)
sample.setValue(32767);
/* else (must be less than 0) so set it to
* the highest negative value
*/
else
sample.setValue(-32768);
SoundSample[] sampleArray =
this.getSamples();
SoundSample sample = null;
}
// loop through the sample values
for (int i = 0; i < sampleArray.length; i++)
{
// get the current sample
sample = sampleArray[i];
}
Georgia Institute of Technology
Using for-each instead
• Replace the following
SoundSample sample = null;
// loop through the sample values
for (int i = 0; i < sampleArray.length; i++)
{
// get the current sample
sample = sampleArray[i];
• With
for (SoundSample sample : sampleArray)
{
Georgia Institute of Technology
Exercise
• Copy the code for forceToExtremes and put it in
Sound.java in the bookClasses directory
• Test it with the following in the main:
String file = FileChooser.pickAFile();
Sound s = new Sound(file);
s.explore();
s.play();
s.forceToExtremes()
s.explore();
s.play();
• Change the code in forceToExtremes to use a
for-each loop instead and test again
Georgia Institute of Technology
Exercise
•
Modify a method in the Picture class that modifies all pixels in a picture to use a for-each loop
public void negate()
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int redValue, blueValue, greenValue = 0;
// loop through all the pixels
for (int i = 0; i < pixelArray.length; i++)
{
// get the current pixel
pixel = pixelArray[i];
// get the current red, green, and blue values
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
// set the pixel's color to the new color
pixel.setColor(new Color(255 - redValue,
255 - greenValue,
255 - blueValue));
}
}
Georgia Institute of Technology
Generics
• Allow you to specify the type of objects in
a collection, both when you declare it and
create it
– Eliminates the need to downcast
– Syntax:
• CollectionType<Type> name = new
CollectionType<Type>;
– List<String> nameList = new ArrayList<String>();
• CollectionType<Type,Type> name = new
CollectionType<Type,Type>;
– Map<String,String> phoneMap = new
HashMap<String,String>();
Georgia Institute of Technology
Without Generics
• You have to cast back to the original class
when you get an object back from a
collection
– Downcast from Object
name = (String) iterator.next();
• With generics you don't have to downcast
– Just give the type on declarations and
creation
name = iterator.next();
Georgia Institute of Technology
Exercise
• Modify the SlideShow class in
examples/SlideShow-List
• Use generics instead of casting
– Specify the type on all declarations of the List
• Including parameters
– Specify the type on the creating of the List
– Remove the cast to Picture when you show
the pictures or get the picture
– You can use a for-each loop instead of an
iterator when showing the pictures
Georgia Institute of Technology
Exercise
• Modify the PhoneBook class in
examples/PhoneBook to use generics for the
phoneMap
– Copy the code to change first
– Then comment out the old code
– Add new code for generics
• Be sure to specify the type when you declare the
phoneMap and when you create it
• Remove the downcast on getting items from the
map
Georgia Institute of Technology
Automatic Boxing and Unboxing
• Private types are not objects:
– int, double, char, boolean
– They can not be added to collections
• You can have arrays of primitive types
– But, they can be wrapped and added to collections
(Integer, Double, Character, Boolean)
• And then you have to unwrap to get the value
• With automatic boxing and unboxing you don't
have to worry about the wrapping and
unwrapping
– It is done for you
Georgia Institute of Technology
Example of Boxing and Unboxing
• Add a primitive type to a collection
– Old way: wrapping the primitive type
•
•
•
•
•
List intList = new ArrayList();
intList.add(new Integer(5));
intList.add(new Integer(6));
Integer integerValue = (Integer) intList.get(0);
int value = integerValue.intValue();
– New: auto wrapping (boxing)
•
•
•
•
List intList = new ArrayList();
intList.add(5);
intList.add(6);
int value = (Integer) intList.get(0);
Georgia Institute of Technology
New Queue Interface
• Java 5.0 adds a Queue interface
– Implemented by the LinkedList class
– Implemented by the PriorityQueue class
• There are no more special AP interfaces
for AB data structures
– Only using the Java interfaces and classes
• Stack (class), Queue (interface), PriorityQueue
(class)
– Using new method names for queues
» peek, add, and remove
Georgia Institute of Technology
Not Covered on Exam
• Enumerated Types
– Old: declare constants using numbers
• public static final int MALE = 0;
• public static final int FEMALE = 1;
– New: use enumerated types
• public enum Gender { MALE, FEMALE };
• Static Imports
– Old: Use class name to access constants
• Color.BLACK
– New: import static java.awt.*;
• BLACK
• Formatted Input and Output
– Use java.io.Scanner class to read from a file or System.in
– See System.out.printf() to format output
Georgia Institute of Technology
Summary
• Java 5.0 adds many new features
– Tested on the Exam
• For-each loop
– for (Type varName : collectionName)
• Generics
– Give the type when you declare and create collection
objects
– List<String> nameList = new ArrayList<String>();
– Eliminates the need to downcast from Object
• Auto-boxing and unboxing
– Don't need to explicitly add primitive values to wrapper
classes and get the value back out
Georgia Institute of Technology