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

Java 5 Part 1

  • Upload
    onawa

  • View
    28

  • Download
    1

Embed Size (px)

DESCRIPTION

Java 5 Part 1. CSE301 University of Sunderland Harry Erwin, PhD. 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: - PowerPoint PPT Presentation

Citation preview

Page 1: Java 5 Part 1

Java 5 Part 1

CSE301

University of Sunderland

Harry Erwin, PhD

Page 2: Java 5 Part 1

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 Developer’s Notebook, O’Reilly

– Flanagan, 2005, Java in a Nutshell, 5th edition, O’Reilly

Page 3: Java 5 Part 1

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

Page 4: Java 5 Part 1

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.

Page 5: Java 5 Part 1

Queues• import Java.util.Queue; to use queues.• That gives you a nice first-in-first-out data structure.• Don’t 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 isn’t as efficient as a C++ priority queue.)

• Possible test questions

Page 6: Java 5 Part 1

Overriding return types

• In correct C++ implementations—I believe Visual C++ has finally fixed this—you 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 you’re compiling Java 5 code.

• Possible test question.

Page 7: Java 5 Part 1

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.

Page 8: Java 5 Part 1

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.

Page 9: Java 5 Part 1

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 you’re 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!

Page 10: Java 5 Part 1

Why Generics?

• This is pre-Java 5, legal, and unsafeList 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.

Page 11: Java 5 Part 1

The Java 5 Solution

• This is legal only in Java 5 and much more safe:List<String> listOfStrings = getListOfStrings();

for(Iterator<String> 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.

Page 12: Java 5 Part 1

Class Syntax for Generic

• class Foo<E> {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 Foo<N> is Foo itself. Sorry!

• E can be used in static member methods.

Page 13: Java 5 Part 1

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.

• List<int> will fail.• List<Integer> will work.

Page 14: Java 5 Part 1

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. Map<F, B> 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, you’re playing with fire.

Page 15: Java 5 Part 1

Parameterized Types as Method Arguments

• private void Foo(Bar<Baz> argument);– This works fine.

• private Bar<Baz> Foo();– Returns an object of class Bar<Baz>.

• private void Foo(Bar<?> argument);– Can read but not write objects of type Quux<?> internally,

and you cannot construct an object of type Quux<?>.

• private void Foo(Bar<N extends Baz> arg);– Is limited to N where N extends Baz. Similarly for class

definitions and implements.

• Bar<Object> is not Bar<?>.

Page 16: Java 5 Part 1

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 LinkedList<Float> is a List<Float>, but not a LinkedList<Number>. (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.

Page 17: Java 5 Part 1

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!