Java 1.5.0

New Features and Enhancements
J2SE 5.0

 

Version 1.5.0 or 5.0?

•      Both version numbers "1.5.0" and "5.0" are used to identify this release of the Java 2 Platform Standard Edition.

•      Version "5.0" is the product version, while "1.5.0" is the developer version.

•      The number "5.0" is used to better reflect the level of maturity, stability, scalability and security of the J2SE.

 

New Java Language Features

•      Generics - adds compile-time type safety to the collections framework and eliminates the need to cast when reading elements from collections.

•      Enhanced for loop - eliminates the need for explicit iterators when iterating over collections.

•      Autoboxing/unboxing - automatically converts primitives (such as int) to wrapper classes (such as Integer) when inserting them into collections, and converts wrapper class instances to primitives when reading from collections.

 

Using Generics

•      Instead of using:

class Test {
   public static void main (String[ ] args)
     {   LinkedList ys = new LinkedList();
          ys.add("zero");    ys.add("one");
         String y = (String)ys.iterator().next();
} }

•      Use

class Test {
public static void main (String[ ] args)
{    LinkedList<String> ys = new LinkedList<String>();
     ys.add("zero");    ys.add("one");
     String y = ys.iterator().next();
} }

Why the generic form is safer

•      The cast to String on the last line of the first example is an example of the typecasting issues that generic types aim to prevent.

–    The issue is that the 1.4.2 Collection API uses the Object class to store the Collection objects, which means that it cannot pick up type mismatches at compile time.

–    The first notification of a problem is a ClassCastException at runtime.

•      The user of a generified API has to simply declare the type used at compile type using the < > notation.

–    No casts are needed and in this example trying to add an Integer object to a String typed collection would be caught at compile time.

 

Java Framework Collection

Purpose of the collection Framework

•      Like C++ STL the purpose is to provide a library with ready-made implementations.

•      Also like the STL, the Collections Framework has three parts

–    Collections themselves

–    Iterators to iterate through the collections

–    Algorithms to be used with the collections

•      But the design decisions are quite different than the STL.  It is based heavily on interfaces and inheritance

–    It was decided to separate the collection interfaces and implementation

–    But most interfaces have an abstract class with some of the methods implemented

 

The collection framework consists of:

•      Collection Interfaces

–    these interfaces form the basis of the framework

–    They specify methods that must be implemented in classes that implement the interface

•      Abstract Implementations

–    Partial implementations to make custom collections easier to implement

•      General-purpose Implementations

–    Classes like ArrayList and LinkedList

•      Algorithms

–    Static methods to perform useful functions on collections, such as sorting a list

Interfaces & Abstract classes
(similarities & differences)

•      Abstract classes may contain both attributes (data) and methods(operations or behavior of the data)

–    The methods may or may not be implemented

•      Interfaces contain only methods for specifying common behavior for the classes that implement them

–    The methods are NEVER implemented

•      Both abstract classes & interfaces can be inherited by sub(classes or interfaces)

 

Java predefined Collection interfaces

•      Collection interface

–    One of Collection's methods returns an Iterator

–    List interface is a sub interface of Collection

•    It has methods that apply specifically to lists

•      Iterator interface

–    It has three easy-to-use
methods

–    ListIterator interface
is subinterface Iterator

 

Annotated Outline of Collections Framework

•      The  link above gives a complete list of the components of the Collection Framework

•      Of most use to you at this point will probably be the General-Purpose Implementations  and Algorithms

–   They are ready to use

•      The interfaces and abstract classes are for you to use if the implemented don’t exactly fit your needs

 

Java's implemented classes

•      ArrayList :  implemented via an array

•      LinkedList:  implemented as a linked structure

•      Priority Queue - an unbounded priority queue backed by a heap

•      HashSet:  a set implemented via a hash table

–    A set is a group of elements with no duplicates

•      TreeSet:  a sorted set implemented as a BST

•      HashMap: a map implemented via a hash table

–    A map is an object that maps keys to values, with no duplicate keys

•      TreeMap: a sorted map implemented as a tree

Stack: inherits from vector

 

An example collection class: the ArrayList

•      How is an ArrayList different than an ordinary array?

–    The declaration

•   An ordinaryarray declares the data type and the size

•   In Java 1.4.2  the ArrayList  type is assumed to be Object, and the size is determined by the implementation

–   This means when an object is put into a collection, it partially loses its identity (i.e. the compiler no longer knows what its "real" type is)

•   In Java 1.5  the ArrayList  type is declared in angle brackets, and can be checked by the compiler

–    The methods

–    http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

    

Links to the Java 5.0 documentation

•    Java 1.5.0 Java API index

•      New Features and Enhancements

–    Generics.

–    Enhanced for loop

–    Autoboxing/unboxing

•      Annotated Outline of Collections Framework