A Quick Guide To All New Features On Java-8
Java 8 or also known as JDK 8 or Java 1.8 - was initially released in March 18, 2014. It came with lots of interesting features and was organized in terms of JDK Enhancement Proposals (JEPs).
Interesting features
- Lambda expressions
- Streams
- Optional
- Java Date and Time API
- Concurrency Enhancements
- Java Nashorn JavaScript
1. Lambda Expression
Addition of lambda expressions (closures) and its supporting features,
Includes - method references, enhanced type inference, and virtual extension methods.
|
|
Using method reference
|
|
For more See
2. Streams
Addition of functionality to the Java Collections Framework for bulk operations upon data. This is commonly referenced as filter/map/reduce for Java.” The bulk data operations include both serial (on the calling thread) and parallel (using many threads) versions of the operations.
|
|
As you can see, operations upon data are generally expressed as lambda functions.
For more See
3. Optional
Optional was introduced to prevent NullPointerException.
It is a container object which may or may not contain a non-null value.
If a value is present i.e not null, than isPresent()
will return true and calling get()
will return the value.
|
|
4. New Date and Time API
Prior to Java 8 - packages ( util.Date, util.Calendar, util.TimeZone ) were Not type safe and were mutable - hence could not be used in multithreading.
Java-8 introduces, New Java.time package:
- Clock
- Instant
- LocalDate, LocalDateTime, Year, YearMonth (ISO 8601)
- ZonedDateTime, OffsetDateTime, OffsetTime
It means API under the package java.time. Now provides:
- Support standard time concepts including date, time, instant, and time-zone
- Partial, duration, period, and intervals
- Immutable implementations, Integrate with existing JDK APIs
- Use relevant standards, including ISO-8601, CLDR, and BCP47
- Be based on an explicit time-scale with a connection to UTC
- DateTimeFormatter for converting DateTime to string and vice versa.
Some code sample:
|
|
5. Concurrency Enhancements
Asynchronous task chaining with CompletableFuture. The Future makes use of result of asynchronous task and monitor its progress from outside, using method of Future interface.
The Java-8 new interface and concurrent classes further enhance this concept.
|
|
// … for more see javadocs
The CompletableFuture allows creation and execution of chains of asynchronous tasks, and allows us to automatically process the results of each task upon completion.
|
|
And more enhancements
- Scalable update variables – DoubleAccumulator, DoubleAdder, etc
- ConcurrentHashMap updates – improved scanning support, key computation
- ForkJoinPool improvements – Completion based design for IO bound applications–Thread that is blocked hands work to thread that is running
To understand complete concepts on Java Concurrency See
6. 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. ECMAScript-262 Edition 5.1
7. New command-line tool, jjs
|
|
You can also execute .js file from the Java code
|
|
Similary other enhancements includes
8. Default methods
For providing default method implemention.
|
|
9. Method References
Shorthand syntax for a lambda expression that executes just ONE method.
|
|
10. Encoding and Decoding (Base64)
Java8 also contains class java.util.Base64 for Base64 encryption and decryption. The supported alphabet is specified by Java in RFC 4648 and RFC 2045.
11. StringJoiner:
StringJoiner is used to construct a sequence of characters separated by a delimiter. like comma( , ), hyphen( - ).
|
|
12. Annotation on Java Types:
Pluggable type checkers can be added to parameters, can only be used on type declarations; Classes, methods, variable definitions.
|
|
13. Internationalization Enhancements
- generate locale data files, CLDR support, Locale Mapping.
14. Security
- Better implementation of SecureRandom number generator
- Enhanced Certificate Revocation-Checking API
- HTTP URL Permissions
- Other several security patches and improvements
15. Remove the permanent generation:
- No more need to tune the size of it
- Current objects moved to Java heap or native memory
- Interned strings, Class metadata, Class static variables
- Part of the HotSpot, JRockit convergence
16. Access to parameter names at runtime:
Retrieve parameter names of methods and constructors– At run-time via core reflection.
JavaFX Applications - Support the direct launching of JavaFX applications.
And Much more… Check JEPS Documentation