Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD."— Presentation transcript:

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 Developer’s Notebook, O’Reilly –Flanagan, 2005, Java in a Nutshell, 5 th edition, O’Reilly

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. 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

6 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.

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 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!

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: List listOfStrings = getListOfStrings(); for(Iterator 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 Foo { 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 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. List will fail. List 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. Map 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.

15 Parameterized Types as Method Arguments private void Foo(Bar argument); –This works fine. private Bar Foo(); –Returns an object of class Bar. 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 arg); –Is limited to N where N extends Baz. Similarly for class definitions and implements. Bar is not Bar.

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


Download ppt "Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD."

Similar presentations


Ads by Google