What is Java 8
As we all know, Java is one of the most powerful programming languages. Oracle released a new version of Java called Java SE 8 on March 18, 2014. Developers primarily use this version of Java to create software platforms, which include various improvements to existing programming libraries, JVM, and tools. One of the most significant features introduced in Java 8 is lambda expressions, which we will explore along with other features.
New Features Java SE 8
For Java programming, Java SE 8 offers a plethora of features.
- Lambda expressions
- Stream API
- ForEach() Method
- Default Method
- Date Time API
- Nashorn JavaScript Engine
- Method References
- StringJoiner
1. Lambda Expression
The lambda expression is an anonymous function(a function without a name) that helps write code in a certain functional style. Using an expression to implement the Single Abstract Method (SAM) can result in clear and concise code. Since data can be iterated, filtered, and extracted, it is helpful, especially in the collection library. Lambda expression also helps in reducing the complexity of code. Below is the syntax of a lambda expression:
Syntax:
(Parameters) -> Expression
Code:
( a, b ) -> a + b // Expression takes 2 parameters and return the sum.
2. Stream API
Stream API offers a technique for data processing in different ways, such as filtering, extracting, transformation, etc., with the help of the package java.util.stream. There is no need to confuse Java.util.streams with Java InputStreams or OutputStreams as there is no relation between them. One of the main advantages of the Stream API is that it maintains the immutability of its source. In other words, the Stream API creates a new data set containing the filtered elements when data is filtered without modifying the original source. The Stream API evaluates the code only when necessary and avoids iterating the code more than once. If reiteration is required, a new stream needs to be generated. Several pre-defined methods are present to support this. To iterate the stream, the following code can be used.
Code:
Stream.iterate(1, elem->elem+1)
3. ForEach() Method
The Iterable interface in Java SE8 introduces a new method called forEach for iterating over the Collection Framework. You can utilize the forEach() loop in a collection class that extends the Iterable interface. As this method takes a single parameter, it can also accept a lambda expression as a parameter.
Code:
age.forEach( age -> { System.out.println(age); }); //each age is taken and printed
4. Default Method
Usually, interfaces cannot have non-abstract methods added to them. But, in the case of Java 8, it permits adding those methods in interfaces. These methods are written with a keyword default known as default methods. Since they are non-abstract methods, the method body can also be included. Interestingly, this feature also ensures binary compatibility with older versions of code.
Code:
public interface Moveable {
default void Sound(){
System.out.println("Hi Anna, How is my sound?");
}}
5. Date Time API
In Java 8, a new Time API and Date API have been introduced, where handling dates are in a different method than other versions of Java. People commonly refer to these classes as JSR-310 or ThreeTen.
The following are the Time and Date classes that are available in Java.time package:
- Jtime.LocalDate class
- LocalTime class
- LocalDateTime class
- MonthDay class
- OffsetTime class
- OffsetDateTime class
- Clock class
- ZonedDateTime class
- ZoneId class
- ZoneOffset class
- Year class
- YearMonth class
- Period class
- Duration class
- Instant class
- DayOfWeek enum
- Month enum
Code:
Clock cl = Clock.systemDefaultZone();
System.out.println(cl.getZone());
6. Nashorn Javascript Engine
Nashorn is a JavaScript engine that helps in executing JavaScript code in Java Virtual Machine (JVM) dynamically. It can be done using the two methods mentioned below.
- With the help of the command-line tool jjs.
- By setting in it into Java source code.
The following steps can be performed to execute using the jjs command-line tool.
- Create a .js file js.
- Write and save the following code into the file.
Code:
var welcome = function(){
print("welcome to Nashorn Javascript Engine");
};
welcome ();
- The open command line terminal.
- Write the command jjs welcome.js and click enter.
- After executing the command, the displayed output will be as follows.
Output:
7. Method References
Method References is another feature introduced in Java 8 that can be used in functional interface methods. In another way, it can be said that they are a subset of another Java lambda expression. A method reference can be used in cases where a lambda expression is applicable.
Methods can be:
- Reference to Constructor
- Reference to the Static method
- Reference to an instance method
The reference to the Static method can be as shown below
Code:
Thread t=new Thread(ExampleClass::ThreadStatusMethod);
t.start();
8. StringJoiner
Java 8 in the Java.util package introduces a new final class called StringJoiner. It allows the construction of a sequence of characters separated by delimiters such as commas (,), hyphens (-), and so on.
Code:
StringJoiner Names = new StringJoiner("-"); // Here, delimiter is -
// Adding names to StringJoiner
joinNames.add("Anna");
joinNames.add("Adam");
Output:
Security Enhancements
Java SE8 also incorporates various other security enhancements in addition to these features.
- The Public Key Cryptography Standards 11 (PKCS) has been extended to comprise 64-bit support for the Operating system Windows.
- Two new implementations have been introduced for UNIX platforms. It offers blocking and non-blocking behavior.
- The SunJCE provider includes the addition of AES and PBE algorithms, such as PBEWithSHA256AndAES_128 and PBEWithSHA512AndAES_256.
- Java SE 8 supports the Server Name Indication (SNI) extension that extends the TLS/SSL protocols to connect during handshaking for supporting server applications.
Conclusion
Oracle has developed Java SE8 as the latest version of Java, which provides numerous features. One of the most significant features is the lambda expression. This document provides a detailed explanation of the features of Java SE 8 and the security enhancements.
Recommended Articles
This is a guide to What is New in Java 8? Here we discuss the eight different features of the new java 8. You can also go through our other related articles to learn more-