Oracle released a new version of Java as Java 8 in March 18, 2014. It was a revolutionary release of the Java for software development platform. It includes various upgrades to the Java programming, JVM, Tools and libraries.
Java 8 Programming Language Enhancements
Java 8 provides following features for Java Programming:- Lambda expressions,
- Method references,
- Functional interfaces,
- Stream API,
- Default methods,
- Base64 Encode Decode,
- Static methods in interface,
- Optional class,
- Collectors class,
- ForEach() method,
- Parallel array sorting,
- Nashorn JavaScript Engine,
- Parallel Array Sorting,
- Type and Repating Annotations,
- IO Enhancements,
- Concurrency Enhancements,
- JDBC Enhancements etc.
Lambda Expressions
Lambda expression helps us to write our code in functional style. It provides a clear and concise way to implement SAM interface(Single Abstract Method) by using an expression. It is very useful in collection library in which it helps to iterate, filter and extract data.
Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.
Syntax
A lambda expression is characterized by the following syntax.parameter -> expression bodyFollowing are the important characteristics of a lambda expression.
- Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
- Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
- Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
- Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Method References
Method references are a special form of the lambda expression.Since your lambda expressions are doing nothing other than invoking existing behavior (methods), you can achieve the same result by referring to it by name.
::
is used to refer to a method.- Method type arguments are inferred by JRE at runtime from the context it is defined.
Types of Method References
- Static method reference
- Instance method reference of a particular object
- Instance method reference of an arbitrary object of a particular type
- Constructor reference
Static Method Reference
When you refer to the static method of Containing a class, e.g.
ClassName::someStaticMethodName
Instance Method Reference of a Particular Object
When you refer to the instance method of the particular object, you will use the
containingObjectReference::someInstanceMethodName
Instance Method Reference of an Arbitrary Object
When you refer to the instance method of a class with the
ClassName
, you will get the instance method reference of an arbitrary object of a particular type, such asClassName::someInstanceMethod;
Constructor Reference
When you refer to a constructor of a class in lambda, you will get a constructor reference, such as
ClassName::new.
Functional Interface
An Interface that contains only one abstract method is known as functional interface. It can have any number of default and static methods. It can also declare methods of object class.Functional interfaces are also known as Single Abstract Method Interfaces (SAM Interfaces).
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package.
Sr.No. | Interface & Description |
---|---|
1 | BiConsumer<T,U> Represents an operation that accepts two input arguments, and returns no result. |
2 | BiFunction<T,U,R> Represents a function that accepts two arguments and produces a result. |
3 | BinaryOperator<T> Represents an operation upon two operands of the same type, producing a result of the same type as the operands. |
4 | BiPredicate<T,U> Represents a predicate (Boolean-valued function) of two arguments. |
5 | BooleanSupplier Represents a supplier of Boolean-valued results. |
6 | Consumer<T> Represents an operation that accepts a single input argument and returns no result. |
7 | DoubleBinaryOperator Represents an operation upon two double-valued operands and producing a double-valued result. |
8 | DoubleConsumer Represents an operation that accepts a single double-valued argument and returns no result. |
9 | DoubleFunction<R> Represents a function that accepts a double-valued argument and produces a result. |
10 | DoublePredicate Represents a predicate (Boolean-valued function) of one double-valued argument. |
11 | DoubleSupplier Represents a supplier of double-valued results. |
12 | DoubleToIntFunction Represents a function that accepts a double-valued argument and produces an int-valued result. |
13 | DoubleToLongFunction Represents a function that accepts a double-valued argument and produces a long-valued result. |
14 | DoubleUnaryOperator Represents an operation on a single double-valued operand that produces a double-valued result. |
15 | Function<T,R> Represents a function that accepts one argument and produces a result. |
16 | IntBinaryOperator Represents an operation upon two int-valued operands and produces an int-valued result. |
17 | IntConsumer Represents an operation that accepts a single int-valued argument and returns no result. |
18 | IntFunction<R> Represents a function that accepts an int-valued argument and produces a result. |
19 | IntPredicate Represents a predicate (Boolean-valued function) of one int-valued argument. |
20 | IntSupplier Represents a supplier of int-valued results. |
21 | IntToDoubleFunction Represents a function that accepts an int-valued argument and produces a double-valued result. |
22 | IntToLongFunction Represents a function that accepts an int-valued argument and produces a long-valued result. |
23 | IntUnaryOperator Represents an operation on a single int-valued operand that produces an int-valued result. |
24 | LongBinaryOperator Represents an operation upon two long-valued operands and produces a long-valued result. |
25 | LongConsumer Represents an operation that accepts a single long-valued argument and returns no result. |
26 | LongFunction<R> Represents a function that accepts a long-valued argument and produces a result. |
27 | LongPredicate Represents a predicate (Boolean-valued function) of one long-valued argument. |
28 | LongSupplier Represents a supplier of long-valued results. |
29 | LongToDoubleFunction Represents a function that accepts a long-valued argument and produces a double-valued result. |
30 | LongToIntFunction Represents a function that accepts a long-valued argument and produces an int-valued result. |
31 | LongUnaryOperator Represents an operation on a single long-valued operand that produces a long-valued result. |
32 | ObjDoubleConsumer<T> Represents an operation that accepts an object-valued and a double-valued argument, and returns no result. |
33 | ObjIntConsumer<T> Represents an operation that accepts an object-valued and an int-valued argument, and returns no result. |
34 | ObjLongConsumer<T> Represents an operation that accepts an object-valued and a long-valued argument, and returns no result. |
35 | Predicate<T> Represents a predicate (Boolean-valued function) of one argument. |
36 | Supplier<T> Represents a supplier of results. |
37 | ToDoubleBiFunction<T,U> Represents a function that accepts two arguments and produces a double-valued result. |
38 | ToDoubleFunction<T> Represents a function that produces a double-valued result. |
39 | ToIntBiFunction<T,U> Represents a function that accepts two arguments and produces an int-valued result. |
40 | ToIntFunction<T> Represents a function that produces an int-valued result. |
41 | ToLongBiFunction<T,U> Represents a function that accepts two arguments and produces a long-valued result. |
42 | ToLongFunction<T> Represents a function that produces a long-valued result. |
43 | UnaryOperator<T> Represents an operation on a single operand that produces a result of the same type as its operand. |
Optional
Java introduced a new class Optional in Java 8. It is a public final class which is used to deal with NullPointerException in Java application. We must import java.util package to use this class. It provides methods to check the presence of value for particular variable.
Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava.
It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach() method to iterate elements.
This method takes a single parameter which is a functional interface. So, you can pass lambda expression as an argument.
With Java 8, a new Date-Time API is introduced to cover the following drawbacks of old date-time API.
Class Declaration
Following is the declaration for java.util.Optional<T> class −public final class Optional<T> extends Object
Class Method
Sr.No. | Method & Description |
---|---|
1 | static <T> Optional<T> empty() Returns an empty Optional instance. |
2 | boolean equals(Object obj) Indicates whether some other object is "equal to" this Optional. |
3 | Optional<T> filter(Predicate<? super <T> predicate) If a value is present and the value matches a given predicate, it returns an Optional describing the value, otherwise returns an empty Optional. |
4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) If a value is present, it applies the provided Optional-bearing mapping function to it, returns that result, otherwise returns an empty Optional. |
5 | T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. |
6 | int hashCode() Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. |
7 | void ifPresent(Consumer<? super T> consumer) If a value is present, it invokes the specified consumer with the value, otherwise does nothing. |
8 | boolean isPresent() Returns true if there is a value present, otherwise false. |
9 | <U>Optional<U> map(Function<? super T,? extends U> mapper) If a value is present, applies the provided mapping function to it, and if the result is non-null, returns an Optional describing the result. |
10 | static <T> Optional<T> of(T value) Returns an Optional with the specified present non-null value. |
11 | static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. |
12 | T orElse(T other) Returns the value if present, otherwise returns other. |
13 | T orElseGet(Supplier<? extends T> other) Returns the value if present, otherwise invokes other and returns the result of that invocation. |
14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier. |
15 | String toString() Returns a non-empty string representation of this Optional suitable for debugging. |
forEach
Java provides a new method forEach() to iterate the elements. It is defined in Iterable and Stream interfaces.It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach() method to iterate elements.
This method takes a single parameter which is a functional interface. So, you can pass lambda expression as an argument.
Date/Time API
Java has introduced a new Date and Time API since Java 8. The java.time package contains Java 8 Date and Time classes.With Java 8, a new Date-Time API is introduced to cover the following drawbacks of old date-time API.
- Not thread safe − java.util.Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable and does not have setter methods.
- Poor design − Default Date starts from 1900, month starts from 1, and day starts from 0, so no uniformity. The old API had less direct methods for date operations. The new API provides numerous utility methods for such operations.
- Difficult time zone handling − Developers had to write a lot of code to deal with timezone issues. The new API has been developed keeping domain-specific design in mind.
- Local − Simplified date-time API with no complexity of timezone handling.
- Zoned − Specialized date-time API to deal with various timezones.
Default Methods
Java provides a facility to create default methods inside the interface. Methods which are defined inside the interface and tagged with default keyword are known as default methods. These methods are non-abstract methods and can have method body.Nashorn JavaScript Engine
Nashorn is a JavaScript engine. It is used to execute JavaScript code dynamically at JVM (Java Virtual Machine). Java provides a command-line tool jjs which is used to execute JavaScript code.You can execute JavaScript code by two ways:
- Using jjs command-line tool, and
- By embedding into Java source code.
StringJoiner
Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc.Stream API
Java 8 java.util.stream package consists of classes, interfaces and an enum to allow functional-style operations on the elements. It performs lazy computation. So, it executes only when it requires.Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement.
SELECT max(salary), employee_id, employee_name FROM EmployeeThe above SQL expression automatically returns the maximum salaried employee's details, without doing any computation on the developer's end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone.
To resolve such issues, Java 8 introduced the concept of stream that lets the developer to process data declaratively and leverage multicore architecture without the need to write any specific code for it.
What is Stream?
Stream represents a sequence of objects from a source, which supports aggregate operations. Following are the characteristics of a Stream −- Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
- Source − Stream takes Collections, Arrays, or I/O resources as input source.
- Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.
- Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.
- Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.
Generating Streams
With Java 8, Collection interface has two methods to generate a Stream.- stream() − Returns a sequential stream considering collection as its source.
- parallelStream() − Returns a parallel Stream considering collection as its source.
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
forEach
Stream has provided a new method ‘forEach’ to iterate each element of the stream. The following code segment shows how to print 10 random numbers using forEach.Random random = new Random(); random.ints().limit(10).forEach(System.out::println);
map
The ‘map’ method is used to map each element to its corresponding result. The following code segment prints unique squares of numbers using map.List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); //get list of unique squares List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
filter
The ‘filter’ method is used to eliminate elements based on a criteria. The following code segment prints a count of empty strings using filter.List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); //get count of empty string int count = strings.stream().filter(string -> string.isEmpty()).count();
limit
The ‘limit’ method is used to reduce the size of the stream. The following code segment shows how to print 10 random numbers using limit.Random random = new Random(); random.ints().limit(10).forEach(System.out::println);
sorted
The ‘sorted’ method is used to sort the stream. The following code segment shows how to print 10 random numbers in a sorted order.Random random = new Random(); random.ints().limit(10).sorted().forEach(System.out::println);
Parallel Processing
parallelStream is the alternative of stream for parallel processing. Take a look at the following code segment that prints a count of empty strings using parallelStream.List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); //get count of empty string long count = strings.parallelStream().filter(string -> string.isEmpty()).count();It is very easy to switch between sequential and parallel streams.
Collectors
Collectors are used to combine the result of processing on the elements of a stream. Collectors can be used to return a list or a string.List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); System.out.println("Filtered List: " + filtered); String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", ")); System.out.println("Merged String: " + mergedString);
Statistics
With Java 8, statistics collectors are introduced to calculate all statistics when stream processing is being done.List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("Highest number in List : " + stats.getMax()); System.out.println("Lowest number in List : " + stats.getMin()); System.out.println("Sum of all numbers : " + stats.getSum()); System.out.println("Average of all numbers : " + stats.getAverage());
Java Base64 Encoding and Decoding
Java provides a class Base64 to deal with encryption and decryption. You need to import java.util.Base64 class in your source file to use its methods.This class provides three different encoders and decoders to encrypt information at each level.
Java Parallel Array Sorting
Java provides a new additional feature in Arrays class which is used to sort array elements parallelly. The parallelSort() method has added to java.util.Arrays class that uses the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays. It is an overloaded method.Java 8 Security Enhancements
1) The Java Secure Socket Extension(JSSE) provider enables the protocols Transport Layer Security (TLS) 1.1 and TLS 1.2 by default on the client side.2) A improved method AccessController.doPrivileged has been added which enables code to assert a subset of its privileges, without preventing the full traversal of the stack to check for other permissions.
3) Advanced Encryption Standard (AES) and Password-Based Encryption (PBE) algorithms, such as PBEWithSHA256AndAES_128 and PBEWithSHA512AndAES_256 has been added to the SunJCE provider.
4) Java Secure Socket Extension (SunJSSE) has enabled Server Name Indication (SNI) extension for client applications by default in JDK 7 and JDK 8 supports the SNI extension for server applications. The SNI extension is a feature that extends the SSL/TLS protocols to indicate what server name the client is attempting to connect to during handshaking.
5) The SunJSSE is enhanced to support Authenticated Encryption with Associated Data (AEAD) algorithms. The Java Cryptography Extension (SunJCE) provider is enhanced to support AES/GCM/NoPadding cipher implementation as well as Galois/Counter Mode (GCM) algorithm parameters.
6) A new command flag -importpassword is added to the keytool utility. It is used to accept a password and store it securely as a secret key. Classes such as java.security.DomainLoadStoreParameter andjava.security.PKCS12Attribute is added to support DKS keystore type.
7) In JDK 8, the cryptographic algorithms have been enhanced with the SHA-224 variant of the SHA-2 family of message-digest implementations.
8) Enhanced support for NSA Suite B Cryptography which includes:
- OID registration for NSA Suite B cryptography algorithms
- Support for 2048-bit DSA key pair generation and additional signature algorithms for 2048-bit DSA keys such as SHA224withDSA and SHA256withDSA.
- Lifting of the keysize restriction from 1024 to 2048 for Diffie-Hellman (DH) algorithm.
- Two new implementations has introduced for UNIX platforms, which provide blocking and non-blocking behavior.
11) The Public Key Cryptography Standards 11 (PKCS) has been expanded to include 64-bit supports for Windows.
12) Two new rcache types are added to Kerberos 5. Type none means no rcache at all, and type dfl means the DFL style file-based rcache. Also, the acceptor requested subkey is now supported. They are configured using the sun.security.krb5.rcache and sun.security.krb5.acceptor.subkey system properties.
13) In JDK 8, Kerberos 5 protocol transition and constrained delegation are supported within the same realm.
14) Java 8 has disabled weak encryption by default. The DES-related Kerberos 5 encryption types are not supported by default. These encryption types can be enabled by adding allow_weak_crypto=true in the krb5.conf file.
15) You can set server name to null to denote an unbound server. It means a client can request for the service using any server name. After a context is established, the server can retrieve the name as a negotiated property with the key name SASL.BOUND_SERVER_NAME.
16) Java Native Interface (JNI) bridge to native Java Generic Security Service (JGSS) is now supported on Mac OS X. You can set system property sun.security.jgss.native to true to enable it.
17) A new system property, jdk.tls.ephemeralDHKeySize is defined to customize the ephemeral DH key sizes. The minimum acceptable DH key size is 1024 bits, except for exportable cipher suites or legacy mode (jdk.tls.ephemeralDHKeySize=legacy).
18) Java Secure Socket Extension (JSSE) provider honors the client's cipher suite preference by default. However, the behavior can be changed to respect the server's cipher suite preference by calling SSLParameters.setUseCipherSuitesOrder(true) over the server.
Java 8 Tools Enhancements
1) A jjs command is introduced, which invokes the Nashorn engine either in interactive shell mode, or to interpret script files.2) The java command is capable of launching JavaFX applications, provided that the JavaFX application is packaged correctly.
3) The java command man page (both nroff and HTML) has been completely reworked. The advanced options are now divided into Runtime, Compiler, Garbage Collection, and Serviceability, according to the area that they affect. Several previously missing options are now described. There is also a section for options that were deprecated or removed since the previous release.
4) New jdeps command-line tool allows the developer to analyze class files to determine package-level or class-level dependencies.
5) You can access diagnostic commands remotely, which were previously accessible only locally via the jcmd tool. Remote access is provided using the Java Management Extensions (JMX), so diagnostic commands are exposed to a platform MBean registered to the platform MBean server. The MBean is the com.sun.management.DiagnosticCommandMBean interface.
6) A new option -tsapolicyid is included in the jarsigner tool which enables you to request a signed time stamp from a Time Stamping Authority and attach it to a signed JAR file.
7) A new method java.lang.reflect.Executable.getParameters is included which allows you to access the names of the formal parameters of any method or constructor. However, .class files do not store formal parameter names by default. To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option of the javac compiler.
8) The type rules for binary comparisons in the Java Language Specification (JLS) Section 15.21 will now be correctly enforced by javac.
9) In this release, the apt tool and its associated API contained in the package com.sun.mirror have been removed.
Javadoc Enhancements
In Java SE 8, the following new APIs were added to the Javadoc tool.- A new DocTree API introduce a scanner which enables you to traverse source code that is represented by an abstract syntax tree. This extends the Compiler Tree API to provide structured access to the content of javadoc comments.
- The javax.tools package contains classes and interfaces that enable you to invoke the Javadoc tool directly from a Java application, without executing a new process.
- The "Method Summary" section of the generated documentation of a class or interface has been restructured. Method descriptions in this section are grouped by type. By default, all methods are listed. You can click a tab to view methods of a particular type (static, instance, abstract, concrete, or deprecated, if they exist in the class or interface).
- The javadoc tool now has support for checking the content of javadoc comments for issues that could lead to various problems, such as invalid HTML or accessibility issues, in the files that are generated by javadoc. The feature is enabled by default, and can also be controlled by the new -Xdoclint option.
Pack200 Enhancements
The Java class file format has been updated because of JSR 292 which Supports Dynamically Typed Languages on the Java Platform.The Pack200 engine has been updated to ensure that Java SE 8 class files are compressed effectively. Now, it can recognize constant pool entries and new bytecodes introduced by JSR 292. As a result, compressed files created with this version of the pack200 tool will not be compatible with older versions of the unpack200 tool.
Java 8 I/O Enhancements
In Java 8, there are several improvements to the java.nio.charset.Charset and extended charset implementations. It includes the following:- A New SelectorProvider which may improve performance or scalability for server. The /dev/poll SelectorProvider continues to be the default. To use the Solaris event port mechanism, run with the system property java.nio.channels.spi.Selector set to the value sun.nio.ch.EventPortSelectorProvider.
- The size of <JDK_HOME>/jre/lib/charsets.jar file is decreased.
- Performance has been improvement for the java.lang.String(byte[], ∗) constructor and the java.lang.String.getBytes() method.
Java 8 Networking Enhancements
1) A new class java.net.URLPermission has been added. It represents a permission for accessing a resource defined by a given URL.2) A package jdk.net has been added which contains platform specific socket options and a mechanism for setting these options on all of the standard socket types. The socket options are defined in jdk.net.ExtendedSocketOptions.
3) In class HttpURLConnection, if a security manager is installed, and if a method is called which results in an attempt to open a connection, the caller must possess either a "connect"SocketPermission to the host/port combination of the destination URL or a URLPermission that permits this request.
If automatic redirection is enabled, and this request is redirected to another destination, the caller must also have permission to connect to the redirected host/URL.
Java 8 Concurrency Enhancements
The java.util.concurrent package added two new interfaces and four new classes.Java.util.concurrent Interfaces
Interface | Description |
---|---|
public static interface CompletableFuture.AsynchronousCompletionTask | It is a marker interface which is used to identify asynchronous tasks produced by async methods. It may be useful for monitoring, debugging, and tracking asynchronous activities. |
public interface CompletionStage<T> | It creates a stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. |
Java.util.concurrent Classes
Class | Description |
---|---|
public class CompletableFuture<T> extends Object implements Future<T>, CompletionStage<T> | It is aFuture that may be explicitly completed, and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion. |
public static class ConcurrentHashMap.KeySetView<K,V> extends Object implements Set<K>, Serializable | It is a view of a ConcurrentHashMap as a Set of keys, in which additions may optionally be enabled by mapping to a common value. |
public abstract class CountedCompleter<T> extends ForkJoinTask<T> | A ForkJoinTask with a completion action performed when triggered and there are no remaining pending actions. |
public class CompletionException extends RuntimeException | It throws an exception when an error or other exception is encountered in the course of completing a result or task. |
New Methods in java.util.concurrent.ConcurrentHashMap class
ConcurrentHashMap class introduces several new methods in its latest release. It includes 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.New classes in java.util.concurrent.atomic
Latest release introduces scalable, updatable, variable support through a small set of new classes DoubleAccumulator, DoubleAdder, LongAccumulator andLongAdder. It internally employ contention-reduction techniques that provide huge throughput improvements as compared to Atomic variables.Class | Description |
---|---|
public class DoubleAccumulator extends Number implements Serializable | It is used for one or more variables that together maintain a running double value updated using a supplied function. |
public class DoubleAdder extends Number implements Serializable | It is used for one or more variables that together maintain an initially zero double sum. |
public class LongAccumulator extends Number implements Serializable | It is used for one or more variables that together maintain a running long value updated using a supplied function. |
public class LongAdder extends Number implements Serializable | It is used for one or more variables that together maintain an initially zero long sum. |
New methods in java.util.concurrent.ForkJoinPool Class
This class has added two new methods getCommonPoolParallelism() and commonPool(), which return the targeted parallelism level of the common pool, or the common pool instance, respectively.Method | Description |
---|---|
public static ForkJoinPool commonPool() | It returns the common pool instance. |
Public static int getCommonPoolParallelism() | It returns the targeted parallelism level of the common pool. |
New class java.util.concurrent.locks.StampedLock
A new class StampedLock is added which is used to add 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.Class | Description |
---|---|
public class StampedLock extends Object implements Serializable | This class represents a capability-based lock with three modes for controlling read/write access. |
Java API for XML Processing (JAXP) 1.6 Enhancements
In Java 8, Java API is added for XML Processing (JAXP) 1.6. It requires the use of the service provider loader facility which is defined by java.util.ServiceLoader to load services from service configuration files.The rationale for this is to allow for future modularization of the Java SE platform where service providers may be deployed by means other than JAR files and perhaps without the service configuration files.
Java Virtual Machine Enhancements
The verification of invokespecial instructions has been tightened so that only an instance initialization method in the current class or its direct super class may be invoked.Java Mission Control 5.3 is included in Java 8
Java Mission Control (JMC) is an advanced set of tools that enables efficient and detailed data analysis and delivers advanced, unobtrusive Java monitoring and management. JMC provides sections for common analysis areas such as code performance, memory and latency.Babel Language Packs in Japanese and Simplified Chinese are now included by default in the Java Mission Control that is included in the JDK 8.
Java 8 Internationalization Enhancements
1) Unicode Enhancements
The JDK 8 includes support for Unicode 6.2.0. It contains the following features.- 733 new characters including Turkish Lira sign.
- 7 new scripts:
- Meroitic Hieroglyphs
- Meroitic Cursive
- Sora Sompeng
- Chakma
- Sharada
- Takri
- Miao
- 11 new blocks: including 7 blocks for the new scripts listed above and 4 blocks for the following existing scripts:
- Arabic Extended-A
- Sundanese Supplement
- Meetei Mayek Extensions
- Arabic Mathematical Alphabetical Symbols
Adoption of Unicode CLDR Data and the java.locale.providers System Property
The Unicode Consortium has released the Common Locale Data Repository (CLDR) project to "support the world's languages, with the largest and most extensive standard repository of locale data available." The CLDR is becoming the de-facto standard for locale data. The CLDR's XML-based locale data has been incorporated into the JDK 8 release, however it is disabled by default.There are four distinct sources for locale data:
- CLDR represents the locale data provided by the Unicode CLDR project.
- HOST represents the current user's customization of the underlying operating system's settings. It works only with the user's default locale, and the customizable settings may vary depending on the OS, but primarily Date, Time, Number, and Currency formats are supported.
- SPI represents the locale sensitive services implemented in the installed SPI providers.
- JRE represents the locale data that is compatible with the prior JRE releases.
Java 8 New Calendar and Locale APIs
The JDK 8 includes two new classes, several new methods, and a new return value for an existing static method.Two new abstract classes for service providers are added to the java.util.spi package.
Class | Description |
---|---|
public abstract class CalendarDataProvider extends LocaleServiceProvider | It is an abstract class for service providers that provide locale-dependent Calendar parameters. |
public abstract class CalendarNameProvider extends LocaleServiceProvider | It is an abstract class for service providers that provide localized string representations (display names) of Calendar field values. |
Method | Description |
---|---|
public static final DecimalFormatSymbols getInstance(Locale locale) | It is used to get the DecimalFormatSymbols instance for the specified locale. This method provides access to DecimalFormatSymbols instances for locales supported by the Java runtime itself as well as for those supported by installed DecimalFormatSymbolsProvider implementations. It throws NullPointerException if locale is null. |
Method | Description |
---|---|
public boolean isSupportedLocale(Locale locale) | It returns true if the given locale is supported by this locale service provider. The given locale may contain extensions that should be taken into account for the support determination. It is define in java.util.spi.LocaleServiceProvider class |
public String getCalendarType() | It returns the calendar type of this Calendar. Calendar types are defined by the Unicode Locale Data Markup Language (LDML) specification. It is defined in java.util.Calendar class. |
Specifier | Description |
---|---|
public static final int SHORT_FORMAT | It is a style specifier for getDisplayName and getDisplayNames indicating a short name used for format. |
public static final int LONG_FORMAT | It is a style specifier for getDisplayName and getDisplayNames indicating a long name used for format. |
public static final int SHORT_STANDALONE | It is a style specifier for getDisplayName and getDisplayNames indicating a short name used independently, such as a month abbreviation as calendar headers. |
public static final int LONG_STANDALONE | It is a style specifier for getDisplayName and getDisplayNames indicating a long name used independently, such as a month name as calendar headers. |
Method | Description |
---|---|
public boolean hasExtensions() | It returns true if this Locale has any extensions. |
public Locale stripExtensions() | It returns a copy of this Locale with no extensions. If this Locale has no extensions, this Locale is returned itself. |
Method | Description |
---|---|
public static List<Locale> filter(List<Locale.LanguageRange> priorityList,Collection<Locale> locales) | It returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647. This is equivalent to filter(List, Collection, FilteringMode) when mode is Locale.FilteringMode.AUTOSELECT_FILTERING. |
public static List<Locale> filter(List<Locale.LanguageRange> priorityList,Collection<Locale> locales, Locale.FilteringMode mode) | It returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647. |
Method | Description |
---|---|
public static List<String> filterTags(List<Locale.LanguageRange> priorityList, Collection<String> tags) | It returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647. This is equivalent to filterTags(List, Collection, FilteringMode) when mode is Locale.FilteringMode.AUTOSELECT_FILTERING. |
public static List<String> filterTags(List<Locale.LanguageRange> priorityList, Collection<String> tags, Locale.FilteringMode mode) | It returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647. |
Method | Description |
---|---|
public static Locale lookup(List<Locale.LanguageRange> priorityList, Collection<Locale> locales) | It returns a Locale instance for the best-matching language tag using the lookup mechanism defined in RFC 4647. |
Public static String lookupTag(List<Locale.LanguageRange> priorityList,Collection<String> tags) | It returns the best-matching language tag using the lookup mechanism defined in RFC 4647. |
Other Java 8 Version Enhancements
Enhancements in JDK 8u5
1) The frequency in which the security prompts are shown for an application has been reduced.Enhancements in JDK 8u11
1) An option to suppress offers from sponsors when the JRE is installed or updated is available in the Advanced tab of the Java Control Panel.2) The Entry-Point attribute can be included in the JAR file manifest to identify one or more classes as a valid entry point for your RIA(Rich Internet application).
Enhancements in JDK 8u20
1) The javafxpackager tool has been renamed to javapackager. This tool has been enhanced with new arguments for self-contained application bundlers.Follwing enhancements are related to the java tool:
- An experimental JIT compiler option related to Restricted Transactional Memory (RTM) has been added.
- Several options related to string deduplication have been added.
- Several options related to Advanced Encryption Standard (AES) intrinsics have been added.
- Combinations of garbage collection options have been deprecated.
Enhancements in JDK 8u31
1) In this release, the SSLv3 protocol is removed from the Java Control Panel Advanced options.Enhancements in JDK 8u40
Java tool
1) The -XX:+CheckEndorsedAndExtDirs has been added because the endorsed-standards override mechanism (JDK-8065675) and the extension mechanism (JDK-8065702) have been deprecated. The option helps identify any existing uses of these mechanisms and is supported in JDK 7u80 and JDK 8u40.2) Java Flight Recorder (JFR) offers a variety of ways to unlock commercial features and enable JFR during the runtime of an application.
It includes java command line options such as jcmd diagnostic commands and Graphical User Interface (GUI) controls within Java Mission Control. This flexibility enables you to provide the appropriate options at startup, or interact with JFR later.
3) The option -XX:StartFlightRecording=parameter=value has a new parameter, dumponexit={true|false}, which specifies whether a dump file of JFR data should be generated when the JVM terminates in a controlled manner.
4) The options related to Restricted Transactional Memory (RTM) are no longer experimental. These options include -XX:RTMAbortRatio=abort_ratio, -XX:RTMRetryCount=number_of_retries, -XX:+UseRTMDeopt, and -XX:+UseRTMLocking.
5) In Java 8, Application Class Data Sharing (AppCDS) has been introduced. AppCDS extends CDS (Class Data Sharing) to enable classes from the standard extensions directories and the application class path to be placed in the shared archive. This is a commercial feature and is no longer considered experimental.
6) New options -XX:+ResourceManagement and -XX:ResourceManagementSampleInterval=value have been added.
7) Additional information about large pages has been added. Large Pages, also known as huge pages, are memory pages that are significantly larger than the standard memory page size. Large pages optimize processor Translation-Lookaside Buffers. The Linux options -XX:+UseHugeTLBFS, -XX:+UseSHM, and -XX:+UseTransparentHugePages have been documented.
8) The option -XX:ObjectAlignmentInBytes=alignment has been documented.
JJS tool
1) The option --optimistic-types=[true|false] has been added. It enables or disables optimistic type assumptions with deoptimizing recompilation.2) The option --language=[es5] has been added to the jjs tool. It specifies the ECMAScript language version.
Javapackager tool
1) New arguments are available for OS X bundlers. The mac.CFBundleVersion argument identifies the internal version number to be used.2) The mac.dmg.simple argument indicates if DMG customization steps that depend on executing AppleScript code are skipped.
0 Comments