Java has gracefully evolved over the past decade or so. However, the biggest update to Java ecosystem is the Java 8 update. The changes ensure that Java stays at the top priority for developers to create enterprise-level applications. The updates brought changes to the language and the JVM (Java Virtual Machine).
Technology is evolving every single day as new libraries, updates, and software are released faster than ever. Developers, on the other hand, are curious in nature and try new technologies as soon as they are released. Furthermore, their jobs require them updating themselves on a regular basis. The new technologies get good reception from the Internet as new articles, videos, and courses are posted very fast.
Java is a versatile language that can be used to create a wide range of applications. Ankira, for example, is developing a new Metroid 2D game in Java. He lives in Alicante, Spain, and is currently studying multimedia engineering at the University of Alicante. Check out his work below.
There are tons of updates in the Java 8 release, but not everything is relevant for Java developers. Moving on, let's check out what's new in Java 8! The article is aimed at Java developers with some prior experience and not for complete beginners.
So without delay, let's get started with Java 8 Programming
1. Lambda expressions
Lambda expressions are the biggest features integrated into Java 8. This functional programming paradigm was, until now, missing from Java. Its inclusion now will only help Java grow in the right direction.
So what exactly is a lambda expression? A lambda expression acts as an anonymous function, and helps write light syntax code. A lambda expression is extremely useful if the function is used only once. It makes code look cleaner and improves readability.
Let's look at one example.
(String s1, String s2, String s3) -> { return s2.length() - s3.length() + s1.length(); }
Lambda expressions can be tricky to understand. Watch chase1263070 play with lambda expressions below.
2. Streams
Another major update in Java 8 is the Stream interface. And yes, it is different from InputStream and OutputStream, so don't get confused.
The Stream interface is located in java.util.Stream and offers parallel operation compared to using an iterator.
The Stream interface comes with different types of stream operations including Filter, Sorted, Match, Map, Count, Reduce, etc. Streams can effectively be used with the lambda expression. Streams can be created with Collection class (java.util.Collection) and then used with Streams interface for better data manipulation.
Let's see an example of the sorted function using Stream interface.
List<String> Str = new ArrayList<>();
Str.add(“abc1”);
Str.add(“aaa1”);
Str
.stream()
.sorted()
.filter((s) -> s.startsWith(“a”))
.forEach(System.out::println);
Output: “aaa1”, “abc1”
3. Maps
Maps API saw new exciting changes in Java 8. The only drawback is that it cannot be used directly with Stream API. The new change includes support for various methods for common tasks, including removing keys, merging entries, and much more.
Let's look at an example of merging entries.
map.merge(15, “fifteen”, (old, newVal) -> old.contact(newVal));
map.get(15);
Output: fifteen
map.merge(15, “merge”, (old, newVal) -> old.concat(newVal));
map.get(15);
Output: fifteenmerge
You can read more about Maps in Java 8 here.
4. Date APIs
The Date API is a new addition to Java 8. Before Date APIs, Developers needed to use Joda time library, but now everything works out of the box. The new Date API borrows heavily from the Joda time library and also rectifies issues found with the Joda library. The Date API is available under the package java.time
Let's see Date API in action below.
//getting local time of Brazil East zone.
LocalTime loc1 = LocalTime.now(ZoneId.of(“Brazil.East”));
// getting clock time from the machine using default time zone.
Clock clock = Clock.systemDefaultZone();
Read more about Java 8 Date APIs here.
5. Annotations
Annotations is already a part of Java, but the Java 8 release saw a change in how annotations work. Annotations act as metadata and can be used as information to the compiler, for runtime processing, for deployment-time, or compile-time processing.
With Java 8, repeatable annotations is now a possibility. This means that you can use already declared annotations with the @Repetable annotation. To do so, you need to use the @Repetable annotations within another annotation. The reason behind the approach is backward-compatibility.
@interface Power {
Power[] value();
}
@Repeatable(Power.class)
@interface Power {
String value();
}
6. Nashorn
Nashorn is the new JavaScript engine in Java 8. It replaces the old and trusty Oracle JVM. The Nashorn is aimed to improve the execution of JavaScript code. Not to mention the fact developers can now use JavaScript code in their Java applications without worrying about performance issues.
To run JavaScript dynamically from Java, you need to import two libraries: The javax.script.ScriptEngine and javax.script.ScriptEngineManager. Many other changes were done to the JavaScript scripting, including the ability to evaluate JavaScript with the engine.eval method.
Other changes that you must know
It is not possible to cover all of the many changes done on multiple levels here. However, some of the other important changes that you should know are as follows:
-
Concurrent Accumulators
-
JDBC 4.2
-
Tons of security updates
-
Changes in JavaFX
-
Tools are reworked
-
JavaDoc tool now supports new DocTree API
-
Improvement in Concurrency handling.
You can find all the changes by following the link.
Java 8 has brought necessary changes to the SDK. Anyone who is transiting from Java 7 to Java 8 should go through the above-mentioned points.
So, are you excited about the new changes in Java 8? Let us know in the Comments section below.