New Java Features Advanced Programming Techniques.

Slides:



Advertisements
Similar presentations
01/26/07© 2006, uXcomm Inc. Slide 1 Tuesday Brown Bag with Howard Abrams New Java Features (from Java 5 & 6)
Advertisements

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.
Data Structures & Java Generics Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CISC370: Inheritance James Atlas. June 17, 2008James Atlas - CISC3702 Questions? Assignment 1 Assignment 1 Review Review.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Compilation 2007 Java 1.5 Features Michael I. Schwartzbach BRICS, University of Aarhus.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
Misc Language Features Features that were added relatively recently that are now in common use.
Lesson 5 Miscellaneous language features Swing Programming.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
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.
16-Jul-15 Java Versions of Java Java 1 Java 2 Java 5.0 Oak: Designed for embedded devices Java 1.1: Adds inner classes and a completely new event-handling.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Effective Java: Generics Last Updated: Spring 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.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.
11-Jan-16 Bits and Pieces Some random things in Java.
Enterprise Java Java SE 5 TM Language Feature Enhancement Primary Source: New Features and Enhancements J2SE 5.0
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Bart van Kuik Application Developer Oracle Corporation.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Basics.
Can these new and complex features simplify Java development?
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Seven Lecture Collection 1. 2 Some Legacy Collection Types  ArrayList  Enumeration  Vector  HashTable – self study.
CMSC 202 ArrayList Aug 9, 2007.
OOP Tirgul 12.
Sixth Lecture ArrayList Abstract Class and Interface
Comp1004: Loops and Arrays II
Outline Creating Objects The String Class Packages Formatting Output
Going further Enumerated types Recursion Collections.
using System; namespace Demo01 { class Program
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
Some random things in Java
Data Structures 1 1.
HW-6 Deadline Extended to April 27th
Array List Pepper.
Some random things in Java
Can store many of the same kind of data together
Generics 27-Nov-18.
(and a review of switch statements)
Some random things in Java
CMSC 202 ArrayList Aug 9, 2007.
Arrays and Collections
CMSC 202 ArrayList Aug 9, 2007.
Generic Types and the Java Collections Framework
class PrintOnetoTen { public static void main(String args[]) {
Can store many of the same kind of data together
COP 3330 Notes 4/6.
Some random things in Java
Java 5 New Features 1-May-19.
Generics 2-May-19.
Presentation transcript:

New Java Features Advanced Programming Techniques

Java 1.5 Generics For-Each loop Autoboxing Enums Varargs Static import Annotations uage/index.html

Generics Generics provides a way to communicate the type of a collection to the compiler, so that it can be checked. The compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

Example // Removes 4-letter words from c. Elements must be strings static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (((String) i.next()).length() == 4) i.remove(); } Java 1.4: // Removes the 4-letter words from c static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove(); } Java 1.5:

Checked Collections Automatically generated casts may fail if you accidentally mix types Checked collection wrappers can be used to locate the problem: Set s = Collections.checkedSet(new HashSet (), String.class);

For-Each loop Cleaner way to iterate over collections and arrays Java 1.4Java 1.5 int sum(int[] a) { int result = 0; for (int i : a) result += i; return result; } int sum(int[] a) { int result = 0; for (int i=0; i<a.length; i++) result += i; return result; }

Autoboxing Collections can only store objects, not primitive data types (int, etc.) When you want to store an int in a collection, you box it using a wrapper class Integer When you get the stored element from the collection, you have to unbox it to get your int back Autoboxing does this for you

Example public static void main(String argv[]) { Vector v = new Vector(); v.add(new Integer(1)); v.add(new Integer(2)); for (Iterator i = v.iterator(); i.nextNext(); ) System.out.println(((Integer)i.next()).intValue()); } Java 1.4: public static void main(String argv[]) { Vector v = new Vector(); v.add(1); v.add(2); for (int i : v) System.out.println(i); } Java 1.5:

Enums Java 1.4 and prior did not support enumerations The standard way to represent an enumerated type: public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; What are the problems with this?

Several problems without explicit enums Not typesafe No namespace Brittle Printed values are uninformative

Compare enum Season { WINTER, SPRING, SUMMER, FALL } Full-fledged class Comparable Serializable Printed values not ints