Java 9 Features (Part -1)

Chinmay Venkata Sabbam
art of coding
Published in
7 min readAug 30, 2020

Evaluation of Java:

  1. In java 1.7, things are very slow as compared to other JDK languages like Scala, Kotlin. when Java 8 releases, it changes the entire landscape of Java because functional programming introduced with Lambdas, Stream API, and Completable Future.
  2. Till now Java 9 is the biggest release even-though Java 8 changes the entire landscape, because it changed the complete ecosystem of the Java and we can build and deploy the applications with it.
  3. Release cycle of the JAVA: The Release cycle of the java changes from JAVA 9, the oracle is releasing the JAVA every 6 months. it doesn’t mean JAVA is developing every 6 months. Features are taking years to develop with out compromise in testing and quality, what ever the features they would complete at that release point of time, they will release that features Instead of waiting for other features to complete.

Java 9 Features:

I categorized Java 9 Features into 3 categories

  1. Improvements
  2. Additional Features
  3. Advanced Features

In this blog we will Focus on Improvements , Remaining will be published in next part.

Improvements:

1. Private Methods In Interfaces:

  1. Until Java 7, Interface is the body of the bundle of Unimplemented methods.
  2. In Java 8 , Default and static methods are implemented in Interfaces just like classes. Generally in classes, what ever the common code we have used in the methods can be written as private methods.
  3. If we need to use the common code in these static and Default methods in Interface, where we need to write that common code ???
    Java 9 provided the solution with the improvement of private methods in interfaces, it provides two types of private methods
    1. normal private methods
    2. private static methods

private static methods are used in the static methods and normal private methods are used in default methods of Interface

private methods in interfaces

NOTE: I recommend you to not use this feature more in the real time because your interface is blotted with full of code which is not good for understand.

2. Try with Resources:

  1. when the Java was introduced, At that point of time the most trending language is C++. The painful point in C++ is garbage collection.
  2. The advantage and disadvantage of the JAVA is the garbage collection because we don’t know at what time it was invoked.
  3. Generally, Programming requires of two types of garbage collection
    (a) Memory in the Runtime
    (b) External Resources (Files, Database and socket connections etc).
code before java 1.7 to close external resources
output for above code

if we observe the above code, finalize method is not invoked. Here Managing memory in runtime and Managing external resources both are two different things (which are conflict each other) but Oracle developers thought they will solve these conflict things with the single solution which doesn’t work in the long run.
There is anthor method in JDK which is not usable at all i.e is System.gc()
Don’t use this because there is no use in it.

Good News is In JAVA 9, finalize() method is deprecated

In life and also in programming we solve the problems is to create the new problems

Here they solved the Garbage collection for memory in runtime. but they created the problem for external resources.

Java 1.7 provides the solution with try- with- resources

JDk 1.7 to close external Resources
Output for above code
try(Resource res = new Resource())

In 1.7 , They solved the problem with external Resources but it leads to create a anthor problem. we observed the code block, we are making Resource Object is tightly coupled(Bad coding practice).

Due to this tight coupling , it is very difficult to automate the testcases with Mock or Stub

In Java 9 , they solved the problem

code with Java 9 to close external Resources
Output of the above code

In Java9, they solved the External Resource tightly coupling problem.
Now caller can send Resource Object or any extension class of the Resource Object or Resource Mock Object. but it will leads to anthor problem.

still resource object is accessible even it is closed
  1. Before Java 9, Resource Object is not accessible out side the try block, But In Java 9 , Resource Object is still accessible for some time even it is closed. we must be take care while writing the code
object used in try block is not mutable

2. Object used in the try block is final or effectively final and it is not allowed to mutate it .

3. Usage of _ as variable name is completely removed in Java 9

  1. _ is having lot of importance in different programming languages like scala,kotlin etc
  2. In Java 8, usage of _ as variable showed as warning and In Java 9, they completely removed it .
_ removed in java 9

4. Take while and Drop while In Streams

we all know that In Java 8 , Streams API completely changes the landscape of the Java. Before Discuss about the Java9 improvement, let’s understand the existing methods in Java 8, after that we will clearly understand the improvement in Java 9.

Java 8 Stream functionalities
Output of the above program

Observations from the above program :

  1. filter takes a Predicate . It is like a gate, that opens or closes per element
  2. limit takes a integer. it is like a door, that is open. but once it is closed then it may closes for ever
  3. skip takes a integer. it is also like a door, that is closed. but once it is opened that it may opened forever.(opposite to limit).

observe the limit and skip functions , it takes integer as input.

lets think about below use cases.
Use Case: 1

List<Integer> numbers = Arrays.asList(11,12,13,14,51,16,17,81,91)
I need to limit this list until number < 50

Now we need to limit the list based on the condition , not on the number. How we will solve this use case in Java 8 ???
solution with imperative style of coding is simple:

for(int number : numbers){
if(number > 50)
break;
System.out.println(number);
}

How can you solve this use case with FUNCTIONAL PROGRAMMING In Java 8 ???? — it’s not possible in Java 8

To Solve this use case take-While(Predicate) Comes in to the picture in Java 9

numbers.stream()
.takeWhile(e -> e<50)
.forEach(System.out::println);

Use Case:2

List<Integer> numbers = Arrays.asList(11,12,13,14,51,16,17,81,91)
I need to skip this list until number < 50

Now we need to Skip the list based on the condition , not on the number. How we will solve this use case in Java 8 ???
solution with imperative style of coding is simple:

boolean isAllowed = false;
for
(int number : numbers){
if(number < 50 && !isAllowed) {
continue;
}
isAllowed = true;
System.out.println(number);
}

How can you solve this use case with FUNCTIONAL PROGRAMMING In Java 8 ???? — it’s not possible in Java 8

To Solve this use case drop-While(Predicate) Comes in to the picture in Java 9

numbers.stream()
.dropWhile(e -> e<50)
.forEach(System.out::println);
implementation of takewhile and dropwhile
output of the above code

NOTE: break(imperative) = takeWhile(functional)

Haskell vs Java Naming convention

   Haskell ----------------------- JAVA
take(number) ------------------ limit(int)
drop(number) ------------------ skip(int)
takeWhile(Predicate) ---------- takeWhile(Predicate)
dropWhile(Predicate) -------- dropWhile(Predicate)

5. iterate method in streams

Before we discuss about the iterate method, Just Imagine you and your friend start debate between imperative and functional style implementations. you are supporting functional style of coding . Let us see Both of you giving counter implementations in imperative and functional style in Java 8 .

Imperative style :

for(int i=0; i<5; i++) {

}

Functional style :

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

Imperative style :

for(int i=0; i<=5; i++) {

}

Functional Style :

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

Imperative

for(int i=0; i<=5; i= i+2) {

}

Functional style in Java 8 ???? — you lost the debate

  1. To Solve the above use case iterate method comes in to the picture in Java 9
  2. Most Commonly used interfaces in Functional style of Coding is Predicate,Supplier, Consumer and Function
    Let’s See iterate method is how intuitive it is
for(int i=0; i<=5; i= i+2) {

}
for(seed,predicate,function) {
}
//Java 9:
IntStream.iterate(seed,predicate,function)

Imperative and functional style implementations in JAVA 9

// imperative 
for
(int i=0; i<=5; i= i+2) {

}
// functionalIntStream.iterate(0,i-> i<=5, i-> i+2)
.forEach(System.out::println);

Infinite Loop Implementation In Imperative and functional style implementations in Java 9

// imperative 
for
(int i=0; ; i= i+2) {

}
// functionalIntStream.iterate(0,i-> i+2)
.forEach(System.out::println);

Note : In iterate method Predicate is optional Parameter , if you did not mention Predicate , it becomes an infinite loop

In the Blog (Part-2) we concentrated on additional features of Java 9 in

In the Upcoming Blog (Part-3) we will concentrate on advanced features of Java 9

  • JShell
  • Modularization (it changes the entire landscape of Java)
  • Stack Walker API

Stay Tuned…….

Follow this Publication for timely updates

Medium :

https://medium.com/art-of-coding

Linked in :

https://www.linkedin.com/company/art-of-coding/

--

--