Recently, Oracle's blog Brian Goetz posted a
link to a presentation from Devoxx talk on Language / Library / VM Co-Evolution in Java SE 8.
In the article I will try to briefly reveal the main problems that will be solved in the G8, as well as their solutions, which were chosen by Oracle. The remaining aspects can be viewed in the original.
Read on:
Extend existing API with backward compatibility;
Java becomes even more object-oriented;
Lambda expressions in java;
Simplify multithreading.
So, what Oracle wants to do:
I. Extend existing API with backward compatibility.
In order to simplify life for all javistates, it was decided to extend / supplement the JDK, as well as to give everyone the opportunity (including existing frameworks) to supplement their APIs. In this case, it is imperative that the legacy code be maintained. To do this, the guys from Oracle have made virtual extension methods (Virtual extension methods).
Virtual extension methods are methods that can be added to existing interfaces and provide a default implementation of these methods, while the implementation classes will not require recompilation and will work as they did before. We obtain the preservation of the health of existing APIs on the one hand and the ability to extend the functionality on the other.
')
Example - Iterator and UnsupportedOperationException.
We all know the Iterator interface's remove method. The javadoc says that if the iterator implementation does not support the remove method, then it can throw an UnsupportedOperationException. Agree, crooked. If I am not going to implement the method, then why should I define it and throw an exception?
Decision:
interface Iterator<T> { boolean hasNext(); T next(); void remove() default { throw new UnsupportedOperationException(); }; }
This defines the default implementation for the remove method. Now implementation classes are not required to provide an implementation of this method.
An example is an iteration through the list.
Suppose there is a code that determines the highest score among students for 2011:
List<Student> students = ... double highestScore = 0.0; for (Student s: students) { if (s.getGradYear() == 2011) { if (s.getScore() > highestScore) { highestScore = s.score; } } }
At first glance, a fairly tolerable code. But it is not object oriented. The iteration of the list itself occurs in a 'for' loop in the client code that uses this list. You can add virtual methods to collections and rewrite this code using an object-oriented approach and an “internal” iteration.
SomeCoolList<Student> students = ... double highestScore = students.filter(new Predicate<Student>() { public boolean op(Student s) { return s.getGradYear() == 2011; } }).map(new Mapper<Student, Double>() { public Double extract(Student s) { return s.getScore(); } }).max();
Here, the SomeCoolList class performs the iteration. But you must admit, this is very cumbersome and verbose. And here lambda expression comes to the rescue.
Decision:
SomeCoolList<Student> students = ... double highestScore = students.filter(Student s -> s.getGradYear() == 2011) .map(Student s -> s.getScore()) .max();
This approach has another advantage - it is easier to parallelize execution, since the collection itself runs through the collection itself.
Ii. Simplify multithreading
Since now computers with multi-core processors have already become the norm, it is obvious that it makes sense to use these cores, i.e. write multi-threaded programs. Therefore, the next improvement in JDK 8 is the simplification of concurrency.
The code above only using multithreading and virtual extension methods can be rewritten as follows:
SomeCoolList<Student> students = ... double highestScore = students.parallel() .filter(Student s -> s.getGradYear() == 2011) .map(Student s -> s.getScore()) .max();
On it the main major features of the eight come to an end.
I also remind you that you can download JDK Standard Edition 8 Developer Preview with Lambda Support
here .