Java new Features Enhancements J2SE / JDK 8/7/6/5 – Main Changes

1. Generics
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

When you see the code , read it as “of Type”; the declaration above reads as “Collection of String c.” The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses. More importantly, we have moved part of the specification of the method from a comment to its signature, so the compiler can verify at compile time that the type constraints are not violated at run time. Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time. The net effect of using generics, especially in large programs, is improved readability and robustness.

Enhancements in Java SE 8

* Lambda Expressions enable you to encapsulate a single unit of behavior and pass it to
other code. You can use a lambda expressions if you want a certain action
performed on each element of a collection, when a process is completed, or when
a process encounters an error. Lambda expressions are supported by the
following features:

* Method References are compact,
easy-to-read lambda expressions for methods that already have a name.

* Default Methods enable you to add new
functionality to the interfaces of your libraries and ensure binary
compatibility with code written for older versions of those interfaces. They
are interface methods that have an implementation and the default keyword at
the beginning of the method signature. In addition, you can define static
methods in interfaces.

* New and Enhanced APIs That Take Advantage
of Lambda Expressions and Streams in Java SE 8 describe new and enhanced
classes that take advantage of lambda expressions and streams.

* Improved Type Inference -
The Java compiler takes advantage of target typing to infer the type parameters
of a generic method invocation. The target type of an expression is the data
type that the Java compiler expects depending on where the expression appears.
For example, you can use an assignment statement's target type for type
inference in Java SE 7. However, in Java SE 8, you can use the target type for
type inference in more contexts. The most prominent example is using a method
invocation's target types to infer the data types of its arguments.

Consider the following example:

List stringList = new ArrayList<>();

stringList.add("A");

stringList.addAll(Arrays.asList());

Disregarding generics for the moment, the method addAll expects a
Collection instance

as its argument, and the method Arrays.asList returns a List
instance. This works because List is a subtype of Collection. Now
considering generics, the target type of addAll is Collection, and
Arrays.asList returns a List instance. In this example, the Java SE 8 compiler can
infer that the value of the type variable T is String. The compiler infers this
from the target type Collection.

Compilers from Java SE 7 and earlier do
not accept this code because they do not use target typing to infer types for
method call arguments. For example, the Java SE 7 compiler generates an error
message similar to the following:

Consequently, in situations like this where the Java compiler
cannot infer types, you must explicitly specify values for type variables with
type witnesses. For example, the following works in Java SE 7:

List stringList = new ArrayList<>();

stringList.add("A");

stringList.addAll(Arrays.asList());

See the following sections in the Java Tutorials for more
information:

o Target Typing in Lambda Expressions

o Type Inference

* Annotations on Java Types - It is now possible
to apply an annotation anywhere a type

is used. Used in conjunction with a pluggable type system, this
allows for stronger type

checking of your code. For more information, see Type Annotations
and Pluggable

Type Systems in the new Annotations lesson in the Java Tutorial.

* Repeating Annotations - It is now possible to
apply the same annotation type more than

once to the same declaration or type use. For more information,
see Repeating

Annotations in the new Annotations lesson in the Java Tutorial.

* Method Parameter Reflection - You can obtain the
names of the formal parameters of

any method or constructor with the method
java.lang.reflect.Executable.getParameters. (The

annotations @SafeVarargs and
@SuppressWarnings({"unchecked", "varargs"}) to

suppress these warnings.

The try-with-resources Statement - The
try-with-resources statement is

a try statement that declares one or more resources. A resource is
an object that must

be closed after the program is finished with it. The
try-with-resources statement

ensures that each resource is closed at the end of the statement.

Any object that implements the new java.lang.AutoCloseable interface or the
java.io.Closeable interface can be used as a resource. The classesjava.io.InputStream,
OutputStream, Reader, Writer, java.sql.Connection, Statement, and ResultSet
have been retrofitted to implement the AutoCloseable interface and can all be
used as resources in a try-with-resources statement.

The Java Collections Framework has been updated to support lambda expressions, streams,
and aggregate operations. For more information on these topics, see the following pages:

 Enhancements in Java SE 8
 New and Enhanced APIs That Take Advantage of Lambda Expressions and Streams in
 Lambda Expressions (The Java Tutorials)
 Aggregate Operations (The Java Tutorials)
 Collections (The Java Tutorials)Performance Improvement for HashMaps with Key Collisions
As part of the work for JEP 180, there is a performance improvement for HashMap objects
where there are lots of collisions in the keys. The alternative String hash function added in 7u6 has been removed from JDK 8, along with the jdk.map.althashing.threshold system property. Instead, hash bins containing a large number of colliding keys improve performance by storing their entries in a balanced tree instead of a linked list. This JDK 8 change applies only to HashMap, LinkedHashMap, and ConcurrentHashMap.

In rare situations, this change could introduce a change to the iteration order
of HashMap and HashSet. A particular iteration order is not specified for HashMap objects -
any code that depends on iteration order should be fixed.Note that, other than removing the feature introduced in 7u6, the java.util.Hashtable class
is not affected. The features added in 7u6 applied to WeakHashMap and Hashtable (and by
extension Properties and Provider) but in JDK 8 these have been removed.Concurrency Utilities Enhancements in Java SE 8
New classes and interfaces in java.util.concurrent
The java.util.concurrent package contains two new interfaces and four new classes:

 Interface CompletableFuture.AsynchronousCompletionTask: A marker interface
identifying asynchronous tasks produced by async methods.
 Interface CompletionStage: A stage of a possibly asynchronous computation, that
performs an action or computes a value when another CompletionStage completes.
 Class CompletableFuture: A Future that may be explicitly completed (setting its
value and status), and may be used as a CompletionStage, supporting dependent
functions and actions that trigger upon its completion.
 Class ConcurrentHashMap.KeySetView: A view of a ConcurrentHashMap as
a Set of keys, in which additions may optionally be enabled by mapping to a common
 Class CountedCompleter: A ForkJoinTask with a completion action performed when
triggered and there are no remaining pending actions.
 Class CompletionException: Exception thrown when an error or other exception is
encountered in the course of completing a result or task.New methods in java.util.concurrent.ConcurrentHashMap
The Collections Framework has undergone a major revision in Java 8 to add aggregate
operations based on the newly added streams facility and lambda expressions. As a result,
the ConcurrentHashMap class introduces over 30 new methods in this release. These include
various forEach methods (forEach, forEachKey, forEachValue, and forEachEntry), search
methods (search, searchKeys, searchValues, and searchEntries) and a large number of
reduction methods (reduce, reduceToDouble, reduceToLong etc.)

Other miscellaneous methods (mappingCount and newKeySet) have been added as well. As
a result of the JDK 8 changes, ConcurrentHashMaps (and classes built from them) are now
more useful as caches. These changes include methods to compute values for keys when
they are not present, plus improved support for scanning (and possibly evicting) entries, as
well as better support for maps with large numbers of elements.New classes in java.util.concurrent.atomic
Maintaining a single count, sum, etc. that is updated by possibly many threads is a common
scalability problem. This release introduces scalable updatable variable support through a
small set of new classes (DoubleAccumulator,DoubleAdder, LongAccumulator, LongAdder),
which internally employ contention-reduction techniques that provide huge throughput
improvements as compared to Atomic variables. This is made possible by relaxing atomicity
guarantees in a way that is acceptable in most applications.
 DoubleAccumulator: One or more variables that together maintain a running double
value updated using a supplied function.
 DoubleAdder: One or more variables that together maintain an initially zero double sum.
 LongAccumulator: One or more variables that together maintain a running long value
updated using a supplied function.
 LongAdder: One or more variables that together maintain an initially zero long sum.
New methods in java.util.concurrent.ForkJoinPool

A static commonPool() method is now available and appropriate for most applications. The
common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool.
Using the common pool normally reduces resource usage (its threads are slowly reclaimed
during periods of non-use, and reinstated upon subsequent use). Two new methods
(getCommonPoolParallelism() and commonPool()) have been added, which return the
targeted parallelism level of the common pool, or the common pool instance, respectively.

New class java.util.concurrent.locks.StampedLock
A new StampedLock class adds a capability-based lock with three modes for controlling
read/write access (writing, reading, and optimistic reading). This class also supports
methods that conditionally provide conversions across the three modes.