Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

55 minutes – 55 slides Ignite Format No stopping! No going back! Questions? Sure, but only if and until time remains on slide (otherwise, save for later) The PRD for Java 7 had almost 2000 changes, here are 55 of the more relevant ones… 6/27/11

6/27/11 Finally we have support for binary literals.

6/27/11 Support for underscores, that allow you to group together, and make it easier to read the literals.

6/27/11 This show the monthNameToDays method using the JDK 7, where you can use the string in the switch statement, making your code, smaller, clearer, and easier to read.

6/27/11 JDK 7 introduce a solution for the issues presented in the previous slides, the automatic resource management. Now you don&apos;t have to worry about closing your resources, they will be automatically closed whe the try block finish execution. Please note the new definition of the try block, where you include the resources to be managed. New superinterface java.lang.AutoCloseable All AutoCloseable (throws Exception) and by extension java.io.Closeable (throws IOException) types useable with try-with-resources Anything with a void close() method is a candidate JDBC 4.1 retrofitted as AutoCloseable too

With automatic resource management even though there are potentially several levels of nesting for the finally clauses that are not immediately visible the full trace of the exceptions that were thrown will be reported. 6/27/11

6/27/11 Now with JDK 7 you can group under the same catch, several exceptions using the “|&apos; operand. Simpler, cleaner and you can reuse common code.

Prior to Java 7, this code would not compile, the types in throws would have to match the types in catch – foo would have to “ throws Exception ” Java 7 adds support for this as long as try block calls all the exceptions in the throws clause, that the variable in the catch clause is the variable that is rethrown and the exceptions are not caught by another catch block. 6/27/11

6/27/11 JDK 7 introduce the diamond operator, now you only need to specify once the type of elements been hold by the collection, on the left hand side, the rest is taking care for you, as java will “infer” the type that will go on the right hand side. This is more complex than just having the compiler perform string substitution. For certain cases the type to be inserted is not represented by the string in the variable declaration (wildcards are a good example). The compiler must infer the type parameter for the instantiation from the type parameter of the variable declaration. List&lt;? extends A&gt; la; la = new ArrayList&lt;B&gt;(); la = new ArrayList&lt;C&gt;(); la = new ArrayList&lt;D&gt;(); List&lt;? super B&gt; lb; lb = new ArrayList&lt;A&gt;(); //fine lb = new ArrayList&lt;C&gt;(); //will not compile public void someMethod(List&lt;? extends B&gt; lb) { B b = lb.get(0); // is fine lb.add(new C()); //will not compile as we do not know the type of the list, only that it is bounded above by B } public void otherMethod(List&lt;? super B&gt; lb) { B b = lb.get(0); // will not compile as we do not know whether the list is of type B, it may be a List&lt;A&gt; and only contain instances of A lb.add(new B()); // is fine, as we know that it will be a super type of A }

Lets study this code closely as we get an unchecked generic array warning. There is a heavy use of the static method asList from the Arrays class, defined as: public static &lt;T&gt; List&lt;T&gt; asList(T... a) Returns a fixed-size list backed by the specified array. Lets go back to the code. We are defining monthsInTwoLanguages, and we are expecting it to be a List&lt;List&lt;String&gt;&gt;. First we call Arrays.asList(&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;), this will return a List&lt;Strings&gt;, in other words a list with the months in English. Then we call Arrays.asList(&amp;quot;Gennaio&amp;quot;, &amp;quot;Febbraio&amp;quot; )); and again we will get a List&lt;Strings&gt;, in this case it will be a list with the months in Italian. Then we call Arrays.asList(Arrays.asList(&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;), Arrays.asList(&amp;quot;Gennaio&amp;quot;, &amp;quot;Febbraio&amp;quot; )); in this case the call seems to be Arrays.asList( List&lt;String&gt;, List&lt;String&gt;) , from previous paragraph. Then we expect to get as result a List&lt;List &lt;String&gt;&gt; that is the expected type for our definition, so why are we getting this warning? When a generic type is instantiated in java, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. This is done to maintain binary compatibility with Java libraries and applications that were created before generics. Then when we do the first asList call, the only stored information is List, we don&apos;t store any information about String, the type of elements hold by this list. And it&apos;s the same scenario for the second asList call. When we finally get to the 3 rd asList call, know we are returning a new List &lt;List &lt;???&gt;&gt; but we didn&apos;t store any information about String, so the compiler doesn&apos;t know what ??? means, In our case we are sure that we have String types, but because we don&apos;t have any information about it, it could be wrong, the compiler doesn&apos;t have this information, and that&apos;s the reason why we get the warning. There could be a potential issue. 6/27/11

6/27/11 Three key new helper Types new in Java 7 Class java.nio.file.Files Exclusively static methods to operate on files, directories and other types of files Class java.nio.file.Paths Exclusively static methods to return a Path by converting a string or Uniform Resource Identifier (URI) Interface java.nio.file.Path Used for objects that represent the location of a file in a file system, typically system dependent. Typical use case: Use Paths to get a Path. Use Files to do stuff. From a developer’s perspective the biggest change will be to replace use of the File object with the Path object. Since File was not designed to be extended it was not possible to add functionality so a new class was required. In many places this can be used in exactly the same way, but there are a few differences that developers will need to be aware of to use this correctly. In the existing IO API reading the contents of a directory blocks until all entries have been retrieved. If you are reading several millin file names across a network connection (not unusual today) this can take a very longh time. NIO2 now provides support for streaming entries so they can be processed as they arrive. There is also support for filtering as part of the API so that it is easy to list only certin types of file and so on. Symbolic link support is optional and is based on the long standing UNIX semantics. Most of the time the link is treated as a normal file, exceptions to this include the delete operation and WalkFIleTree (although the FOLLOW_LINKS option can be used with this). To provide extensibility the Filesystem class provides an interface to a Filesystem which can be any form of file storage system, for example a ZIP file can be accessed as if it were a filesystem even though it is itself a file on another filesystem. The attribute package provides enhanced access to metadata for files and also solves a longstanding performance problem, namely that every request for an attribute results in a separate stat() call

File copy is really easy With fine grain control File move is supported Optional atomic move supported Finally the most obvious file system operations are supported in a clean consistent way in Java. Copying, moving and renaming files is all performed using a Path for each part of the operation. Three copy options can also be specified to copy the attributes of the file rather than using the defaults, to replace an existing file if one already exists at the destination path and to make the move an atomic operation from the filesystem perspective. The Files class provides a large number of utility methods for typical filesystem operations (copy, move, delete, createDirectory and so on).

DirectoryStream iterate over entries Scales to large directories Uses less resources Smooth out response time for remote file systems Implements Iterable and Closeable for productivity Filtering support Build-in support for glob, regex and custom filters The DirectoryStream class provides an iterator that can be used to read entries from a directory. One point of note is that, as the DirectoryStream provides an Iterator, it is not possible to use the DirectoryStream more than once, since there is no way to reset an Iterator,. There is built in support for regular expressions so complex patterns can be used like *.{c,h,cpp,hpp,java}

Java NIO.2 Watching A Directory Create a WatchService “watcher” for the filesystem Register a directory with the watcher “ Watcher” can be polled or waited on for events Events raised in the form of Keys Retrieve the Key from the Watcher Key has filename and events within it for create/delete/modify Ability to detect event overflows

6/27/11 Phaser Barrier similar to CyclicBarrier and CountDownLatch Used for many threads to wait at common barrier point For example, use this to create N threads that you want to do something simultaneously – “ start gun ” metaphore How is Phaser an improvement? Dynamic add/remove “parties” to be sync’d Better deadlock avoidance Arrival “ counting ” and phase advance options, etc Termination api’s Tiering (tree structure) Rather than sync 100 threads, sync 2x50 then 2x. Java SE 7 includes updates to the concurrency APIs first introduced in Java SE 5. This is an update to an update;the orinial utilities were defined in JSR166. This was eXtended in JSR166x (Java SE 6) and extended further through JSR166y (Java SE 7). Introduces the fork-join framework for fine grained parallelism The Phaser which is a reusable synchronization barrier, similar in functionality to CyclicBarrier and CountDownLatch but supporting more flexible usage.

6/27/11 TransferQueue interface Extension to BlockingQueue Implemented by LinkedTransferQueue Additional Benefits: Adds methods: transfer(E e), tryTransfer(E e), tryTransfer(E e, long timeout), hadWaitingConsumer(), getWaitingConsumerCount() Allows for smarter queues to be built – sidestep the data structure if it ’ s known there are consumers waiting. A TransferQueue is a BlockingQueue in which producers may wait for consumers to receive elements. A TransferQueue may be useful for example in message passing applications in which producers sometimes (using method transfer(E)) await receipt of elements by consumers invoking take or poll, while at other times enqueue elements (via method put) without waiting for receipt. Non-blocking and time-out versions of tryTransfer are also available. A TransferQueue may also be queried, via hasWaitingConsumer(), whether there are any threads waiting for items, which is a converse analogy to a peek operation. Like other blocking queues, a TransferQueue may be capacity bounded. If so, an attempted transfer operation may initially block waiting for available space, and/or subsequently block waiting for reception by a consumer. Note that in a queue with zero capacity, such as SynchronousQueue, put and transfer are effectively synonymous. This is implemented by the LinkedTransferQuue which is an unbounded TransferQueue based on linked nodes. This queue orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time for some producer.

ForkJoinTask The abstract base class for: RecursiveAction A recursive resultless task Implements compute() abstract method to perform calculation RecursiveTask Similar to RecursiveAction but returns a result The main classes for the Fork Join framework: ForkJoinTask – a task to be processed

Number Shaper Enhancements NumericShaper used to map numbers to non Latin char sets (since 1.4) NumericShaper traditionally used an int bitmask for defaults Fine when there were only 19 defaults In Java 7 there are 34 (&gt; 32 bits!!) Java 7 now has an Enum NumericShaper.Range Backward compatibility maintained, new API’s added for Enum use where desired 6/27/11

Locale enhancement – Categories Default Locale can be set independently for format resources (dates, numbers, currencies) and display resources (menus and dialogs) For example, an application for Japanese speakers who deal with US financial transactions may: //Enum Locale.Category – DISPLAY and FORMAT //Default no arg get/set is DISPLAY Locale.setDefault(DISPLAY, Locale.JAPAN); Locale.setDefault(FORMAT, Locale.US); 6/27/11

6/27/11 Better than Metal for cross platform look-and-feel Introduced in Java SE 6u10, now part of Swing Not the default L&amp;F Scalable Java 2D impl The Nimbus look and feel was introduced in Java SE 6 update 10 as a replacement for the metal cross platform L&amp;F. In Java SE 7 this has been moved to a standard part of the platform (previously it was an oracle extension package, now it is in the javax.swing.plaf package). The decision was made not to make this the default L&amp;F, this will continue to be Metal. Although many people in the community wanted this it was decided that the impact on existing applications would be too great.

6/27/11 The Jlayer component provides an easy way to add an overlay to existing Swing components. Examples are a progress wheel or highlighting only vallid choices in a multiple-choice component.

Mixing of AWT and Swing – Works* As of 6u12 and 7u1, some caveats for scroll bars 6/27/11

VM: Compressed OOPS by default Going from 32bit to 64bit system will grow the heap by ~1.5x simply because of bigger ordinary object pointers Memory is cheap, but bandwidth and cache is not Compressed OOPS: Managed 32 bit pointers (similar heap sizes for 32/64 bit apps) Scaled (8 x 4GB chunks) added to a 64 bit base Useful for heaps up to 32GB Compressed OOPS will turn off when –Xmx &gt; 32g 6/27/11

To help understand this we provide a diagram. To shorten this presentation you may want to remove the details of how invokedynamic works as this does not impact on most developers.

6/27/11 Java XML Technology Enhancements JAXP 1.4.5 Bug fixes and performance improvements JAX-WS 2.2.4 Bug fixes and performance improvements JAXB 2.2.3 Bug fixes and performance improvements Here are a few miscellaneous things that don’t fit into the preceeding areas. Some minor changes to cryptography in terms of the underlying algorithm used. Various updates to Java APIs defined in other JSRs Some minor changes to class loading which will only have an impact on a very small number of developers. Finally! Javadocs get CSS support so we can bring javadocs into the 21 st Century.

JavaDoc Improvements in Java 7 Section 508 accessibility guidelines Captions, headings, etc. Previously, JavaDoc wrote to an OutputStream on the fly meaning it built the document sequentially, imposing limitations Now uses internal “ HTMLTree ” classes Generates compliant HTML Allows for more advancements in the future Removes limitation of only being able to execute only once in any VM Was fine when used as a command line tool Continuous build, etc, made it necessary to address this! 6/27/11

CSS for JavaDoc - stylesheet.css 6/27/11

6/27/11 This is the standard Oracle safe-harbour statement. Please allow the audience to read this in their own time.

58.
The preceding is intended to outline our generalproduct direction. It is intended for informationpurposes only, and may not be incorporated into anycontract. It is not a commitment to deliver anymaterial, code, or functionality, and should not berelied upon in making purchasing decisions.The development, release, and timing of anyfeatures or functionality described for Oracle’sproducts remains at the sole discretion of Oracle. 58 58