Pages

Friday 12 September 2014

JAVA 8 What's New About it...?


Not Only Lambdasbut it includes lot more new features

  • Lambda expressions
  • Method references
  • Default Methods (Defender methods)
  • A new Stream API.
  • A new Date/Time API.
  • Nashorn, the new JavaScript engine and more...
Let's look at some of features that we are excited to share with you:

Lambda Expressions:

The biggest feature of Java 8 is its support to lambda expressions.

Lambda expressions are anonymous methods, aimed at mainly addressing the “vertical problem” by replacing the machinery of anonymous inner classes with a lighter-weight mechanism in applicable cases.
As Specified  by JSR 335:  It defines the syntax of Lambda Expressions, including rules for the new kinds of parameters lists and statement lists.

Lambda Expressions are not exactly used to remove Anonymous classes. Lambdas anonymous functions used to decrease the overhead of Anonymous classes.

Lambda expressions in Java is usual written using syntax (argument) -> (body) .
Some more examples:
(int a, int b) -> {  return a + b; }
() -> System.out.println("Hello World");
(String s) -> { System.out.println(s); }
() -> 42
() -> { return 3.1415 };

Method References:

Method references enables you to call method by name, where sometimes we can't create
anonymous methods using Lambda Expressions instead we can just call the method .

Using method references we can refer to methods and constructors without invoking them.

There are four kinds of method references:


KindExample
Reference to a static methodContainingClass::staticMethodName
Reference to an instance method of a particular objectcontainingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular typeContainingType::methodName
Reference to a constructorClassName::new


A non-constructor method reference consists of a qualifier, followed by the :: symbol, followed by an identifier. The qualifier is either a type or an expression, and the identifier is the referenced method's name. The qualifier is a type for static methods, whereas the qualifier is a type or expression for non-static methods. If the qualifier is an expression, the expression is the object on which the method is invoked.
A constructor reference consists of a qualifier, followed by the :: symbol, followed by the keyword new. The qualifier is always a type, and the new keyword is the name of the referenced constructor. The qualifier type must support the creation of instances; for example, it cannot be the name of an abstract class or interface.

Before a non-static method can be invoked, an object on which to invoke this method is required. This target object is known as a receiver. The receiver can be provided as an expression (a bound non-static method) or a type (an unbound non-static method). I'll have more to say about receivers in these contexts later in this article.

Default Methods(Defender Methods) :

A new addition to the Java language, you can now add method bodies to interfaces (called default methods). These methods are implicitly added to every class which implements the interface.

New Stream API:
Stream API enables you the power of parallel processing. You can find the Stream interface in java.util.stream package .
The most obvious way to create a stream is from a Collection.
The Collection interface has two default methods on it for creating streams:
  • stream(): Returns a sequential Stream with the collection as its source.
  • parallelStream(): Returns a possibly parallel Stream with the collection as its source.
The ordering of the Stream relies on the underlying collection just like an Iterator.


Nashorn, the new JavaScript engine: 
From this version (Java 8) ships with new engine Oracle Nashorn which is based on JSR292 and invokedynamic. Until Java 7 JDK's shipped with a javascript engine based on  Mozilla Rhino.

A simple way to get started with Oracle Nashorn is to run JavaScript programs from the command line. To do so, builds of Oracle’s JDK or OpenJDK include a command-line tool called . It can be found in the 
bin/ folder of a JDK installation along with the well-known java,javac, or jar tools.



No comments:

Post a Comment