2. Current status
- Java 8 was released on 18 March 2014
- From October 2014 (Java SE 8 Update 25),
Java 8 has been the default version to
download on java.com
- Oracle will no longer post updates of Java
SE 7 to its public download sites after April
2015
3. Language-level changes
- Lambda expressions
- Functional Interfaces
- References to constructors and methods
- Default and static methods in interfaces
- Repeating annotations, type annotations
- Method parameter reflection
4. Lambda expressions
Lambda expression is an anonymous function that can be
assigned to a variable (not always) and be executed by
calling that variable or directly. Lambda expressions have
access to final variables from the corresponding scope.
For example, you may create the following object:
Comparator<String> strLenComp = (firstStr, secondStr) ->
Integer.compare(firstStr.length(), secondStr.length());
And then you may sort your list using that object:
Collections.sort(myListOfStrings, strLenComp);
5. Functional interfaces
Functional interface is an interface with only
one abstract method.
If your interface is a functional interface then
you can create a lambda function of such type
and pass it whenever you need to. Like in the
previous example.
6. References to constructors and
methods
You may reference a method or a constructor as a lambda-expression
● object::instanceMethod
x::equals is in fact y -> x.equals(y)
● Class::staticMethod
System.out::println is in fact x -> System.out.println(x)
● Class::instanceMethod
String::compareToIgnoreCase is in fact (x, y) ->
x.compareToIgnoreCase(y)
● Class::new
MyClass::new is in fact x -> new MyClass(x)
7. Default and static methods in
interfaces
The interface may declare a default
implementation of a method.
public int compareTo(T other);
public default boolean gt(T other) {
return compareTo(other) > 0;
}
And finally you may have static methods in
interfaces.
8. Repeating annotations, type
annotations
● It’s now possible to repeat annotation several times:
@interface Hints {Hint[] value();}
@Repeatable(Hints.class)@interface Hint {String value();}
@Hint("hint1")@Hint("hint2") class MyClass {}
In the example above class MyClass now automatically has annotation Hints.
● Also now we have two more targets for an annotation:
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
@interface NotNull{}
Examples:
List<@NotNull String> nonEmptyList;
String nonEmptyString = (@NotNull String) maybeEmpty;
9. Method parameter reflection
Now you can obtain the names of the formal
parameters of any method or constructor with
the method
java.lang.reflect.Executable.getPar
ameters if your code is compiled with -
parameters option to the javac compiler.
10. What else has changed
- Stream API
- New date/time API
- Project Nashorn - JavaScript on JVM
- Tens of other small and big changes and
additions
11. Stream API
java.util.Stream is a sequence of elements on which you may perform intermediate (result in that
stream) or terminal (result in something else) operations. Stream can be linear or parallel. The whole
conception of stream comes from Functional Programming.
Example:
List<String> stringCollection = new ArrayList<>();
// add elements to stringCollection
long startsWithB = stringCollection.parallelStream().filter((s) ->
s.startsWith("b")).count();
Maps do not support streams. But now they have some useful methods like putIfAbsent, merge,
getOrDefault.
12. New date/time API
- Inspired by Joda-Time with some important differences
- use class Clock instead of java.util.Date and
System.currentTimeMillis():
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();// time in millis
- use ZoneId to work with time zones
- use LocalTime, LocalDate, LocalDateTime and few other new
classes for purposes that can be easily guessed from their names.
- use DateTimeFormatter for thread-safe date parsing and formatting
13. Project Nashorn
- Compiles JS to JVM bytecode
- JavaScript may now be embedded to a Java
programm
- also allows to develop free standing
JavaScript applications using the jrunscript
command-line tool
14. What else was added that can be
interesting?
- Statically-linked JNI libraries
- Removal of the permanent generation
- JavaFX improvements
- New standard classes and utils ike Arrays.parallelSort,
StampedLock, CompletableFuture
- See the full list of all the changes at
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
15. What’s next
Java 9
- Somewhere in 2016
- Modularization of the JDK, several new APIs
(JSON, HTTP, Processes, etc), compiler and
JDK improvements
- See http://openjdk.java.net/projects/jdk9/ for
the full list of planned features