Introduction to Java 8
As we all know, Java is one of the most powerful programming languages. A new version of Java, Java SE 8 has been released by Oracle on March 18, 2014. This innovative release of Java is mainly used for software development platforms. There are several features in Java 8 which are the updates to the existing version of programming libraries, JVM and Tools. The most important feature is said to be the introduction of lambda expressions which we will see along with other features.
New Java 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
Lambda expression is an anonymous function(a function without a name) that helps in writing code in a certain functional style. Single Abstract Method (SAM) can be implemented in a clear and concise way by using an expression. Since data can be iterated, filtered and extracted, it is very 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 package java.util.stream. There is no need to get confuse java.util.streams with Java InputStreams or OutputStreams as there is no relation between them. One of the main advantages of Stream API is that it does not alter its source. i.e If a set of data is filtered, a new set of data is created with filtered data rather than modifying the original source. Stream API evaluates the code only when it is needed and it does not iterate the code more than once. If reiteration has to be done, a new stream has 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
In order to iterate over the Collection Framework, Java SE8 offers a new method called forEach which is defined in the Iterable interface. ForEach() loop can be used in a collection class that extends the interface Iterable. Since this method inputs a single parameter, a lambda expression can also be passed as a parameter.
Code:
age.forEach( age -> { System.out.println(age); }); //each age is taken and printed
4. Default Method
Normally, Non-abstract methods cannot be added to interfaces. But, in the case of Java 8, it permits to add those methods in interfaces. These methods are written with a keyword default and known as default methods. Since they are non-abstract methods, the method body can also be included. Interestingly, this feature also ensures the 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 has been introduced where handling dates are in a different method compared to other versions of Java. These classes are also called as JSR-310, 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.
In order to execute using the jjs command-line tool, the following steps can be performed.
- 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 command jjs welcome.js and click enter.
- Once the command is executed, the below output will be displayed.
Output:
7. Method References
Method References is another feature that is 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 feature, lambda expressions. It is because a method reference can also be used if a lambda expression can be used.
Methods can be:
- Reference to Constructor
- Reference to 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
A new final class StringJoiner has been added to Java 8 in the java. util package. A sequence of characters can be constructed separated by delimiters like comma (,), hyphen (-), etc.
Code:
StringJoiner Names = new StringJoiner("-"); // Here, delimiter is -
// Adding names to StringJoiner
joinNames.add("Anna");
joinNames.add("Adam");
Output:
Security Enhancements
In addition to these features, several other security enhancements are also done to Java SE8.
- The Public Key Cryptography Standards 11 (PKCS) has been extended to comprise 64-bit support for the Operating system, Windows.
- For UNIX platforms, two new implementations have been presented. It offers blocking and non-blocking behavior.
- In SunJCE provider, AES and PBE algorithms such as PBEWithSHA256AndAES_128 and PBEWithSHA512AndAES_256 are also added.
- 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
Java SE8 is the new version of Java developed by Oracle which offers several features. Lambda expression is considered as the most significant feature among them. In this document, features of Java SE 8 and security enhancements are explained in detail.
Recommended Articles
This is a guide to What’s New in java 8? Here we discuss the different eight features of new java 8. You can also go through our other related articles to learn more-