Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Review Generics and the ArrayList Class
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
Intro-Sound-part31 Introduction to Processing Digital Sounds part 3 Barb Ericson Georgia Institute of Technology Oct 2009.
Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
ArrayList, Multidimensional Arrays
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
int [] scores = new int [10];
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
The ArrayList Data Structure Standard Arrays at High Speed!
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
Manipulating Pictures, Arrays, and Loops part 3
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Generics 27-Nov-18.
Arrays versus ArrayList
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology June 2006
CSE 113 A February , 2009.
Introduction to Processing Digital Sounds part 3
int [] scores = new int [10];
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
February , 2009 CSE 113 B.
Introduction to Processing Digital Sounds
Manipulating Pictures, Arrays, and Loops part 6
Creating and Modifying Text part 3
Review: libraries and packages
Generics 2-May-19.
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

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 046, ,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() { 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]; /* 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); }

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, greenValue, 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 name = new CollectionType ; –List nameList = new ArrayList (); CollectionType name = new CollectionType ; –Map phoneMap = new HashMap ();

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 Primative 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 nameList = new ArrayList (); –Eliminates the need to downcast from Object