Java 9 Improvements-1


There are a couple of improvements in Java 9. We will cover it one by one with the examples of each.

1.) Interface private methods: In Java 9, we can create private methods in interfaces. The use case is very simple: similar to classes where you put the repetitive code in a private method and reuse that in other methods, you can simply create one private helper method in the interface as well, that can be used by other default methods, which helps us to prevent writing boilerplate code.


2.) No _(underscore): In Java 9, underscore is a keyword. As such you can’t use this as a variable name as you can in Java8. Even in Java8, using this as a variable name generates a warning but in Java9 it throws an error. So, the _(underscore ) is no more used as an identifier.

Now, the question is why did they do that?
The answer is very simple: to give a meaningful and appropriate name to the variable.

This will throw a compile time error:  As of release 9, ‘_’ is a keyword, and may not be used as an identifier.

3.) takeWhile: In Java 9, we have a method takeWhile that is similar to limit() in Java 8  with the only difference, that is:
limit() is based on the count value that we are passing to it as a parameter. Let’s take an example.

In the above example, I said the limit is so it will only allow me to pass 4 values.
i.e: 11, 22, 33, 44

Now, let’s take a scenario: what if I want to use limit, not on a fixed number but I want a limit based on a condition?

takeWhile(predicate) is the solution to this problem. It will close the gate as soon as the predicate is false. Let’s go through this with one example:

4.) dropWhile: In Java 9, we have a method dropWhile() that is similar to skip() in Java 8  with the only difference, that is:
skip(count) is based on the count value that we are passing to it as a parameter. Let’s take an example.

In the above example, it will skip the first five values and it will start printing from the 6th value. Same as earlier situation, what if I want to use skip(count) on condition basis, then dropWhile(predicate)  is the solution. Let’s go through this with one example:

5.) IntStream iterate: In Java 8 we have an alternative to for-loop i.e.

IntStream.range(0, 5)
        .forEach(System.out::println);

The above statement does same work as below for-loop.

for(int i=0; i<5; i++){
    System.out.println(i);
}

Let’s take one scenario where I want to increment the counter value in for-loop by 2 each time as below:

for(int i=0; i<5; i = i+2){
    System.out.println(i);
}

In Java 8, this is not really easy to do. Java 9 overcame this kind of situation as below:

In the above example, we can see how we can write code in a functional way to solve this kind of problem. It’s good to know what type of arguments iterate() method takes.

IntStream.iterate(seed, predicate, function)

The first argument is the seed value and the second argument is the predicate and the last argument is a function.

6.) Improved Optional:  In Java8, there were some problems with Optional that we will cover with the below example.

In the above example, there is one limitation, that is, we are writing code in an imperative pattern instead of the functional pattern because we are following best practices instead of using get() on optional. This is the limitation in Java 8.

Now, we will see the example how Java 9 overcomes this problem.

These were just the basic yet powerful new features of Java 9. We will come up with more advanced features such as Jlink and modularity in the next series of this blog.

References: https://www.youtube.com/watch?v=Yacu1yUktjY&t=3498s

knoldus-advt-sticker

This entry was posted in Functional Programming, Java, Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s