Java se7 features

32
May 2012 JAVA SE 7 Features Kumaraswamy Gowda

description

This presentation provides overview of Java SE 7 new features, mainly concentrates on Project Coin, NIO.2 updates

Transcript of Java se7 features

Page 1: Java se7 features

May 2012

JAVA SE 7 Features

Kumaraswamy Gowda

Page 2: Java se7 features

JDK 7 Release – Session agenda

2

Project Coin

NIO.2

Concurrency and collections updates

Performance JDK 7 release

More features from JDK 7 release

A glimpse of Java SE 8

Q & A

Page 3: Java se7 features

Project Coin

3

The Six Coin Features and How They HelpEasier to use generics

DiamondVarargs warnings

More concise error handlingMulti-catchtry-with-resources (aka ARM or TWR)

Consistency and clarityStrings in switchLiteral improvements

Binary literalsUnderscores in literals

Page 4: Java se7 features

Diamond syntax

4

Type Inference for Generic Instance Creation

Page 5: Java se7 features

Multi Catch and Final Re-throw

5

Handling More Than One Type of Exception in a single catch block

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any value to it within the catch block.

Page 6: Java se7 features

Multi Catch and Final Re-throw Contd.

6

Rethrowing Exceptions with More Inclusive Type Checking

Page 7: Java se7 features

try-with-resources

7

Prior JDK 7 code in finally block, to ensure that a resource is closed regardless of whether

the try statement completes normally or abruptly. The code is a bit messy!

Page 8: Java se7 features

try-with-resources Cont.

8

JDK 7 introduces a try-with-resources statement, which ensures that each of the resources in try(resourses) is closed at the end of the statement. This results in cleaner code.

Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

Suppressed Exceptions Throwable.getSuppressed Classes That Implement the AutoCloseable or Closeable Interface

Page 9: Java se7 features

Nothing much to explain here, the title says it all. Previous versions of Java, the argument of switch had to be only of the following primitive data

types: byte, short, char, int, or enum Starting from JDK 7, you can use arguments of type String in the expression of a switch

statement.

How about LONG in switch?

Strings in switch

9

Page 10: Java se7 features

Binary Literals

10

Integral types (byte, short, int, and long) can also be expressed using the binary number system

Add the prefix 0b or 0B to the number

Page 11: Java se7 features

Underscores in Numeric Literals

11

How much is 10000000000?

And now 10_000_000_000?

You can place underscores only between digits; you cannot place underscores in the following places:

At the beginning or end of a number

Adjacent to a decimal point in a floating point literal

Prior to an F or L suffix

In positions where a string of digits is expected

Page 12: Java se7 features

Simplified vararg methods invocation

12

Heap Pollution Non-reifiable types

parameterized types, such as ArrayList<Number> and List<String>

Occurs when a variable of a parameterized type refers to an object that is not of that parameterized type

During type erasure, the types ArrayList<Number> and List<String> become ArrayList and List, respectively.

In JDK 1.7 @SafeVarargs has been introduced to suppress such exception.

Page 13: Java se7 features

vararg methods invocation Contd.

13

Page 14: Java se7 features

Project Coin – Design complexity vs Implementation Effort

14

Page 15: Java se7 features

Project Coin support in IDEs

15

Pick an IDE, any IDEIntelliJ IDEA 10.5 and later

http://blogs.jetbrains.com/idea/2011/02/announcing-intellij-idea-105-with-full-java-7-support/Eclipse 3.7.1 and later

http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.htmlNetBeans 7.0 and later

http://netbeans.org/kb/docs/java/javase-jdk7.html

Page 16: Java se7 features

NIO.2

16

Navigation Helpers

Two key navigation Helper Types: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.

Page 17: Java se7 features

NIO.2 Features –Files Helper Class

17

Class java.nio.file.Files

Exclusively static methods to operate on files, directories and other types of files

Files helper class is feature rich:CopyCreate DirectoriesCreate FilesCreate LinksUse of system “temp” directoryDeleteAttributes –Modified/Owner/Permissions/Size, etc.Read/Write

Files.move(src, dst); Files.copy(src, dst,StandardCopyOption.COPY_ATTRIBUTES,

StandardCopyOption.REPLACE_EXISTING);

Page 18: Java se7 features

NIO.2 Directories

18

Directory support in NIO.2 using DirectoryStream Uses less resources Smooth out response time for remote file systems Implements Iterable and Closeable for productivity

Page 19: Java se7 features

NIO.2 Symbolic Links

19

Path and Files are “link aware”.One could create symbolic link or hard link

from one path to another.

Page 20: Java se7 features

NIO.2 Walking A File Tree

20

A FileVisitor interface makes walking a file tree for search, or performing actions.

SimpleFileVisitor implements

preVisitDirectory(T dir, BasicFileAttributes attrs);

visitFile(T dir, BasicFileAttributes attrs);

visitFileFailed(T dir, IOException exc);

postVisitDirectory(T dir, IOException exc);

Page 21: Java se7 features

NIO.2 Watching A Directory

21

Create a WatchService “watcher” for the filesystem

Register a directory with the watcher

“Watcher” can be polled or waited

on for eventsEvents 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

Page 22: Java se7 features

NIO.2 many more…

22

Custom FileSystemsProvider registration method may depend on JSR-277Provider is a factory for FileSystem, FileRef andFileChannel objectsNeed not be tied to a “real” filesystem

Zip file, CD-ROM image, ram disk, flash rom, etcMultiple/alternate views of same underlying files

Hide sensitive files, read-only views, path munging, etc

Filesystem provider for zip/jar archives

Asynchronous I/O

Buffers, Sockets and File I/OMostly small additions for completenessMulticast is the big new feature

Page 23: Java se7 features

Concurrency and Collections updates

23

platform-independent parallel speed-ups of computation-intensive operations

not a full substitute for the kinds of arbitrary processing supported by Executors or Threads

provide significantly greater performance on multiprocessor platforms

Page 24: Java se7 features

Concurrency: Recursive Task example

24

Page 25: Java se7 features

Concurrency: Recursive Task example Cont.

25

Page 26: Java se7 features

Performance - JDK 7

26

The JDK 7 delivers quite a speed boost over JDK 6 array accesses

Page 27: Java se7 features

More features of Java SE 7

27

Support for dynamically-typed languages (InvokeDynamic)Concurrency and collections updatesSCTP (Stream Control Transmission Protocol)SDP (Sockets Direct Protocol)JDBC 4.1Enhanced MbeansUpdate the XML stackclient

XRender pipeline for Java 2DCreate new platform APIs for 6u10 graphics featuresNimbus look-and-feel for SwingSwing JLayer componentGervill sound synthesizer

and few more…JavaDoc Improvements CSS for JavaDoc -stylesheet.css

Page 28: Java se7 features

Java SE 8

28

Due to land Spring / Summer 2013

Main Features

Lambdas (aka Closures)

New Date and Time APIs

Jigsaw (modularisation)

Also:

Type Annotations

Coin Part 2

Ongoing work to merge VMs

Getting rid of PermGen

Page 29: Java se7 features

Java SE 8 Cont.

29

Advanced Example

Creating immutable collection

Creating collections like Arrays and Index based access

Page 30: Java se7 features

References for resources

30

JDK 7 features: http://openjdk.java.net/projects/jdk7/features/ http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-41

8459.html Project coin: http://docs.oracle.com/javase/7/docs/technotes/guides/language/

enhancements.html#javase7 Support for Dynamically Typed Languages in the Java

Virtual Machine http://java.sun.com/developer/technicalArticles/DynTypeLang/ NIO.2 http://docs.oracle.com/javase/7/docs/technotes/guides/io/enhanc

ements.html#7

Page 31: Java se7 features

References for JDK8

31

Project Lambda http://openjdk.java.net/projects/lambda/

Project Lambda: Straw-Man Proposal http://cr.openjdk.java.net/~mr/lambda/straw-man/

Page 32: Java se7 features

Java 7 New Features

32

Questions?