Java 1.5: Comp-303: Programming Techniques
Java 1.5: Comp-303: Programming Techniques
April 1, 2004
Page 1
Last lecture . . .
The Command design pattern allows you to seperate the Invoker from the Receiver, thus allowing you to create parametrizable frameworks. The Command design pattern also allows you to do nifty things like queing commands, undo, redo, transactions, etc.
April 1, 2004
Page 2
April 1, 2004
Page 3
April 1, 2004
Page 4
Java Specication Requests (JSRs) are the actual descriptions of proposed and nal specications for the Java platform. At any one time there are numerous JSRs moving through the review and approval process. A JSR can take up to 200 days before it is approved by the JCP.
April 1, 2004
Page 5
Page 6
Apache Software Foundation BEA Systems Fujitsu Limited IBM Doug Lea Richard Monson-Haefel Oracle The SCO Group
Apple Borland Hewlett-Packard IONA Technologies Macromedia Nokia Networks SAP Sun Microsystems
April 1, 2004
Page 7
Ericsson Mobile Platforms Insignia Matsushita Nokia Research In Motion Sony Sun Microsystems Texas Instruments
April 1, 2004
Page 8
Java 1.5
Released February 2004, J2SE 1.5, code-named Tiger, is the rst major improvement to the Java programming language since two years ago when version 1.4 was released. Improvements include: enumerated types metadata/autoboxing of primitive types enhanced for loops improved diagnostics and for the rst time the use of generics
April 1, 2004
Page 9
Metadata
The Metadata feature in J2SE 1.5 provides the ability to associate additional data alongside Java classes, interfaces, methods, and elds. This additional data, or annotation, can be read by the javac compiler or other tools, and depending on conguration can also be stored in the class le and can be discovered at runtime using the Java reection API. For example:
@Overrides public boolean equals(Foo that) { ... }
This annotation type allows the programmer to declare his belief that a method declaration overrides a superclass method. The compiler checks whether method actually overrides a superclass method, and reports an error if it does not, nipping the problem in the bud.
April 1, 2004
Page 10
Generic Types
Generic types enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time. Before
ArrayList list = new ArrayList(); list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue();
After
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, new Integer(42)); int total = list.get(0).intValue();
You cant use a primitive as a type variable for your generic collection.
Lecture 23 Comp 303 : Java 1.5
April 1, 2004
Page 11
Autoboxing
Converting between primitive types, like int, boolean, and their equivalent Object-based counterparts like Integer and Boolean, can require unnecessary amounts of extra coding. The autoboxing and auto-unboxing of Java primitives produces code that is more concise and easier to follow. Before
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, new Integer(42)); int total = (list.get(0)).intValue();
After
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, 42); int total = list.get(0);
April 1, 2004
Page 12
After
ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i : list) { ... }
April 1, 2004
Page 13
Enumerated types
This type provides enumerated type when compared to using static nal constants.
public enum StopLight { red, amber, green };
April 1, 2004
Page 14
Static Import
The static import feature, implemented as import static, enables you to refer to static constants from a class without needing to inherit from it. Instead of BorderLayout.CENTER each time we add a component, we can simply refer to CENTER.
import static java.awt.BorderLayout.*; getContentPane().add(new JPanel(), CENTER);
April 1, 2004
Page 15
Formatted Output
Developers now have the option of using printf type functionality to generated formatted output. This will help migrate legacy C applications, as the same text layout can be preserved with little or no change. Most of the common C printf formatters are available, and in addition some Java classes like Date and BigInteger also have formatting rules.
System.out.printf("name count\n"); System.out.printf("%s %5d\n", user,total);
April 1, 2004
Page 16
Formatted Input
The scanner API provides basic input functionality for reading data from the system console or any data stream. The Scanner methods like next and nextInt will block if no data is available. If you need to process more complex input then there are also pattern matching algorithms, available from the java.util.Formatter class. The following example reads a String from standard input and expects a following int value.
Scanner s = Scanner.create(System.in); String param = s.next(); int value = s.nextInt(); s.close();
April 1, 2004
Page 17
Varargs
The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation for the method that accepts the argument list and is used to implement the exible number of arguments required for printf.
void argtest(Object ... args) { for (int i=0;i <args.length; i++) { } } argtest("test", "data");
April 1, 2004
Page 18
Concurrency Utilities
The concurrency utility library is a special release of the popular concurrency package into the J2SE 1.5 platform. It provides powerful, high-level thread constructs, including executors, which are a thread task framework thread safe queues Timers locks (including atomic ones) other synchronization primitives.
April 1, 2004
Page 19
A semaphore can be used to restrict access to a block of code. Semaphores are more exible and can also allow a number of concurrent threads access, as well as allow you to test a lock before acquiring it.
final private Semaphore s= new Semaphore(1, true);
//for non-blocking version use s.acquire() s.acquireUninterruptibly(); balance=balance+10; //protected value s.release(); //return semaphore token
April 1, 2004
Page 20
The 1.5 release promises improvements in scalability and performance with a new emphasis on startup time and memory footprint to make it easier to deploy applications running at top speed. One of the more signicant updates is the introduction of class data sharing in the Hotspot JVM. This technology not only shares read-only data between multiple running JVMs but also improves startup time as core JVM classes are pre-packed.
April 1, 2004
Page 21
April 1, 2004
Page 22
Generating Stack traces has been awkward if no console window has been available. Two new APIs, getStackTrace and Thread.getAllStackTraces provide this information programmatically.
StackTraceElement e[]=Thread.currentThread().getStackTrace(); for (int i=0; i <e.length; i++) { System.out.println(e[i]); } System.out.println("\n"+Thread.getAllStackTraces());
April 1, 2004
Page 23
Other Improvements
Network: The InetAddress class now provides an API to allow testing for the reachability of a host. This features provides a ping-like capability in Java. Security: This release of J2SE oers signicant enhancements for security. Improvements for scalability (SSLEngine) and performance. Internationalization Character handling is now based on version 4.0 of the Unicode standard. And many improvements/bug x in Java Sound, AWT and Swing.
April 1, 2004
Page 24
Desktop Client
The Java Desktop client remains a key component of the Java platform and as such has been the focus of many improvements in J2SE 1.5. This Beta release contains some of the early improvements in startup time and memory footprint. Not only is the release faster but the Swing toolkit enjoys a fresh new theme called Ocean. And by building on the updates in J2SE 1.4.2, there are further improvements in the GTK skinnable Look and Feel and the Windows XP Look and Feel.
April 1, 2004
Page 25
Page 26
Summary
The JCP controls the evolution of the Java Specication. Java 1.5 introduces some interesting new improvements, most of them focused at making life easier for the programmer.
April 1, 2004
Page 27
April 1, 2004
Page 28
References
J2SE 1.5 in a Nutshell http://java.sun.com/developer/technicalArticles/releases/j2se15/ Java 1.5 SDK Documentation http://java.sun.com/j2se/1.5.0/docs/index.html Sun Lights Up Java 1.5 Beta http://www.internetnews.com/dev-news/article.php/3309061 Java Community Process http://www.jcp.org/en/home/index
April 1, 2004
Page 29