Java 5 Part 1 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Java 5 Part 1

Description:

McLaughlin and Flanagan, 2004, Java 1.5 Tiger: A Developer's Notebook, O'Reilly ... In a String, use a pair of char values to encode a 21-bit character. StringBuilder ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 18
Provided by: harry8
Category:
Tags: java | part | tiger

less

Transcript and Presenter's Notes

Title: Java 5 Part 1


1
Java 5 Part 1
  • CSE301
  • University of Sunderland
  • Harry Erwin, PhD

2
Introduction
  • Java 5 has been changed to improve the language.
  • This lecture will discuss the changes. You will
    be tested on your knowledge of Java 5.
  • References include
  • McLaughlin and Flanagan, 2004, Java 1.5 Tiger A
    Developers Notebook, OReilly
  • Flanagan, 2005, Java in a Nutshell, 5th edition,
    OReilly

3
Topics
  • Part 1 (today)
  • Arrays
  • Queues
  • Overriding return types
  • Unicode
  • StringBuilder
  • Generics
  • Part 2 (next lecture)
  • Enumerated types
  • Boxing
  • Annotations
  • for/in
  • Static imports
  • Formatting
  • Threading

4
Arrays
  • Many static utility methods have been added for
    use with arrays. (Possible test questions
    follow!)
  • To use them, import java.util.Arrays
  • Arrays.toString(theArray) lists the array
    contents
  • Arrays.deepToString(theArray)
  • Arrays.equals(array1,array2) tests two arrays
    for equality
  • Arrays.hashCode(theArray) provides a hashcode
    (qv) for the array.
  • Arrays.deepEquals(array1,array2) is similar.
  • Arrays.deepHashCode(theArray) is similar.

5
Queues
  • import Java.util.Queue to use queues.
  • That gives you a nice first-in-first-out data
    structure.
  • Dont use add() or remove() with it, as they
    throw exceptions. Instead, use offer() and
    poll(). offer() returns false if the queue is
    full, and poll() returns null if the queue is
    empty.
  • If you want to see the next available entry in
    the queue without removing it, use element() or
    peek().
  • PriorityQueue is ordered using comparators. (It
    still isnt as efficient as a C priority
    queue.)
  • Possible test questions

6
Overriding return types
  • In correct C implementationsI believe Visual
    C has finally fixed thisyou can have a
    covariant return, where a method in a subclass
    returns a more specialised type instance than the
    overridden method in the base class. This is now
    available in Java 5. The return type of the
    subclass method must be an extension of the
    return type of the base class, and the compiler
    must be told youre compiling Java 5 code.
  • Possible test question.

7
Unicode 4.0
  • Java 5 supports Unicode 4.0, which defines some
    characters that require 21 bits in addition to
    the 16-bit standard Unicode 3.0 characters.
  • You use int to represent these characters, and
    some of the static methods in the Character class
    now accept int arguments to deal with them.
  • In a String, use a pair of char values to encode
    a 21-bit character.

8
StringBuilder
  • This is a drop-in replacement for StringBuffer
    when thread safety is not an issue and speed is.
    (If thread safety matters, use StringBuffer.)
  • StringBuffer and StringBuilder represent
    modifiable Strings, so you can edit them in many
    ways.
  • The toString() method for either converts its
    buffer into a String.
  • To go the other direction, create a StringBuffer
    or StringBuilder object around the String.

9
Generics
  • This is the most important new feature of Java 5.
    This allows a class to indicate that it uses or
    contains instances of some generic type. This
    increases the type safety of Java because the
    compiler can verify that youre using that type
    correctly.
  • A generic class or method looks like a C
    template class or method.
  • Test questions are to be found throughout the
    material on generics!

10
Why Generics?
  • This is pre-Java 5, legal, and unsafe
  • List listOfStrings getListOfStrings()
  • for(Iterator i listOfStrings.iterator()
    i.hasNext())
  • String item (String)i.next()
  • Do something with the String item
  • You cannot remove the cast to String.

11
The Java 5 Solution
  • This is legal only in Java 5 and much more safe
  • ListltStringgt listOfStrings getListOfStrings()
  • for(IteratorltStringgt i listOfStrings.iterator()
    i.hasNext())
  • String item i.next()
  • Do something with the String item
  • The cast to String is gone.
  • Not much change? Wait.

12
Class Syntax for Generic
  • class FooltEgt
  • E can be used as a method argument type, a field
    member, or anywhere that a type can be used in a
    class definition.
  • (The naming convention for a parameter is to use
    a single uppercase letter.)
  • E cannot be used in a static member field as the
    class that is shared among all instances of type
    FooltNgt is Foo itself. Sorry!
  • E can be used in static member methods.

13
Generics and Primitive Types
  • They are incompatible, simply put. E cannot be
    a primitive type. Use the corresponding wrapper
    types (Integer, Boolean, Float, Double,
    Character, etc.) and let autoboxing and unboxing
    (discussed later) handle the conversions.
  • Listltintgt will fail.
  • ListltIntegergt will work.

14
Generics and Collections
  • All the collection classes support generics (in
    addition to their normal use in pre-Java 5 code).
  • Map even takes two parameters. MapltF, Bgt defines
    the keys to the map as type F, and the values as
    type B.
  • Iterators are also parameterized (although they
    can also be nearly avoided using for/in loops,
    discussed later). Note you must parameterize the
    iterator if the collec-tion is parameterized.
  • Finally, if you parameterize the iterator and not
    the collection, youre playing with fire.

15
Parameterized Types as Method Arguments
  • private void Foo(BarltBazgt argument)
  • This works fine.
  • private BarltBazgt Foo()
  • Returns an object of class BarltBazgt.
  • private void Foo(Barlt?gt argument)
  • Can read but not write objects of type Quuxlt?gt
    internally, and you cannot construct an object of
    type Quuxlt?gt.
  • private void Foo(BarltN extends Bazgt arg)
  • Is limited to N where N extends Baz. Similarly
    for class definitions and implements.
  • BarltObjectgt is not Barlt?gt.

16
Type Conversions with Parameterized Types
  • Here be dragons!
  • Parameterized types form a hierarchy, but the
    hierarchy is based on the base type, not on the
    types of the parameters.
  • A LinkedListltFloatgt is a ListltFloatgt, but not a
    LinkedListltNumbergt. (It is a LinkedList.)
  • Why? Generics are a Java 5 add-on, and the class
    files must be Java 1-compatible and forgetting
    about the parameter types. (Inner classes are a
    similar add-on with some unexpected behaviour.)
    To avoid abuse, inheritance ignores parameters.

17
Arrays of Parameterized Types
  • Here be more dragons!
  • An array of type S is also of type T when T
    is a base class or interface of S.
  • If you try to store an object of type T into an
    array of type S via an alias array of type T,
    you get a class cast exception. If the type S
    were parameterized, this protection could not be
    enforced during run-time, as parameterized types
    are new in Java 5.
  • So to prevent this, you are not allowed to create
    an array of a parameterized type! Sorry!
Write a Comment
User Comments (0)
About PowerShow.com