JAVA New Features
New Features in JAVA 8
Java 8 release is the greatest thing in the Java world since Java 5 (released quite a while ago, back in 2004). It brings tons of new features to the Java as a language, its compiler, libraries, tools and the JVM (Java virtual machine) itself.
There are dozens of features added to Java 8, the most significant ones are mentioned below π».
πLambda Expressions
Functional Programming
Lambda expressions provide anonymous function types to Java
– Replace use of anonymous inner classes
– Provide more functional style of programming in Java
doSomething(new DoStuff(){
public boolean isGood(int value){
return value == 42;
}
});
doSomething(answer -> answer == 42);
πExtension Methods
Bringing Multiple Inheritance (of Functionality) to Java
Provide a mechanism to add new methods to existing interfaces
– Without breaking backwards compatibility
– Gives Java multiple inheritance of behaviour, as well as types (but not state!)
public interface Set<T> extends Collection<T> {
public int size();
... // The rest of the existing Set methods
public T reduce(Reducer<T> r)
default Collections.<T>setReducer;
}
πStatic Methods In Interfaces
Previously it was not possible to include static methods in an interface
Static methods, by definition, are not abstract
Static methods, by definition, are not abstract
– @FunctionalInterface can have zero or more static methods
static <T> Predicate<T> isEqual(Object target) {
return (null == target)
Objects::isNull
object -> target.equals(object);
}
πFunctional Interface
Single Abstract Method (SAM) type
A functional interface is an interface that has one abstract method
A functional interface is an interface that has one abstract method
– Represents a single function contract
– Doesn’t mean it only has one method
@FunctionalInterface annotation
– Helps ensure the functional interface contract is honoured
– Compiler error if not a SAM
πMethod References
Method references let us reuse a method as a lambda expression
⇩
FileFilter x = File f -> f.canRead();
⇩
FileFilter x = File::canRead;
πConstructor References
Same concept as a method reference
– For the constructor
⇩
– For the constructor
Factory<List<String>> f = () -> return new ArrayList<String>();
⇩
Factory<List<String>> f = ArrayList<String>::new;
πAnnotations On Java Types
Annotations can currently only be used on type declarations
– Classes, methods, variable definitions
Extension for places where types are used
– Classes, methods, variable definitions
Extension for places where types are used
– e.g. parameters
Permits error detection by pluggable type checkers
– e.g. null pointer errors, race conditions, etc
public void process(@notnull List data) {…};
πAccess To Parameter Names At Runtime
Mechanism to retrieve parameter names of methods and constructors
– At runtime via core reflection
Improved code readability
– At runtime via core reflection
Improved code readability
– Eliminate redundant annotations
Improve IDE capabilities
– Auto-generate template code
Method and Constructor now inherit from new Executable class
– generategetParameters() returns array of Parameter objects
– generateName, type, annotations for each parameter
πParallel Array Sorting
Additional utility methods in java.util.Arrays
– parallelSort (multiple signatures for different primitives)
Anticipated minimum improvement of 30% over sequential sort
– parallelSort (multiple signatures for different primitives)
Anticipated minimum improvement of 30% over sequential sort
– For dual core system with appropriate sized data set
Built on top of the fork-join framework
– ses Doug Lea’s ParallelArray implementation
– Requires working space the same size as the array being sorted
πDate And Time APIs
A new date, time, and calendar API for the Java SE platform
Supports standard time concepts
Uses relevant standards, including ISO-8601, CLDR, and BCP47
Based on an explicit time-scale with a connection to UTC
Supports standard time concepts
– Partial, duration, period, intervals
– time, instant, and time-zone
Provides a limited set of calendar systems and be extensible to others Uses relevant standards, including ISO-8601, CLDR, and BCP47
Based on an explicit time-scale with a connection to UTC
- References..π..
- http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
Tip: Understand the concept well. Then you will be able to develop better applications.
Comments
Post a Comment