When Java 8 was released two years ago, the community graciously accepted it, seeing it as a huge step toward making Java better. Its unique selling point is the attention paid to every aspect of the programming language, including JVM (Java Virtual Machine), the compiler, and other help-system improvements.
Java is one of the most searched programming languages according to Tiobe index for July 2016, where Java ranks number one. Its popularity is also seen on Livecoding, a social live coding platform for engineers around the world, where hundreds and thousands of Java projects are broadcasted live.
So what makes Java 8 amazing? Let's discuss the Top 5 Java 8 features for developers.
1. Lambda expressions
Lambda expressions (or closures) are popular in functional programming. Until now, Java didn't support them and therefore was missing one of the best ways to write code. Even JVM-based languages Scala and Clojure had lambda expressions from day one.
With lambda expressions, functions can be used anywhere, and can be treated as code. Prior to this, Java developers had to use anonymous boilerplate classes with lambdas that can quickly become tedious and hard to maintain.
Using lambda expressions is easy. All you need to do is use a comma-separated list of parameters or collection, and use the symbol “->” within the body. Check the example below to understand the syntax of the lambda expression.
Arrays.asList(“k”,”l”,”m”).forEach( m -> System.out.println(m));
With lambda expression, executing simple loops over collection will become more trivial. Check out chase1263070 as he goes through the motion of lambda expression in Java 8.
3. Date/time APIs
Another great addition to Java 8 is the new date/time API. It borrows heavily from Joda time, which was utilized by Java developers to compensate for the lack of support. The Java API library is complex in nature, and it easily makes developers go haywire. To compensate for the problem, Java 8 rewrites the whole API from scratch.
Another reason for writing the API from scratch is a design flaw in Joda, which the implementers don't want to propagate in lieu of simpler solution. The new APIs are both powerful in nature and simple to use.
Here's an example of the API:
LocalDateTime a1 = LocalDateTime.now(); // this will return current date and time.
LocalDate.parse(“02:53:15”); // Simple string input
4. Stream API
Another new inclusion in Java 8, thanks to lambda syntax, is the Stream API, which enables developers to work with Collections. Both lambda functions and the Stream API facilitate functional programming in Java and helps programmers to write more meaningful code.
So as a developer, how does Stream API help you? It lets you easily work with collections and manipulate them accordingly, such as counting, filtering, etc. Also, lambda functionality helps you to write simpler code. Just be careful not to confuse Stream API with InputStream and OutputStream.
Let's look at a simple example.
List<String> myList =
Arrays.asList(“k1”, “l1”, “m2”, “m3”, “j4”, “j4”, “j1”, “m1”);
.filter(s -> s.endsWith(“1”))
Output: j1, k1, l1,, m1
As you can see in the above example, you can define computation in a series of steps or nested operations, just like Monads.
5. Concurrent accumulators
One of the biggest challenges for the developer is the ability to safely run the code thread. The average developer might not be able to implement thread safe correctly, as there are too many scenarios to look out for, including the need to update the numeric counters that are accessed by multiple threads.
With Java 8, developers can utilize the concurrent accumulator class, which enables them to handle the counters efficiently in a thread safe manner.
Java 8 seems to bring the lost glory of Java over the past few years. The new features will enable developers to write high quality code and help maintain its competitiveness among other programming languages.
There are many features that we didn't cover in the article. You can find the whole list of changes by clicking here. Which features do you think are gamechangers? Comment below and let us know!
This article is published as part of the IDG Contributor Network. Want to Join?