Evolution of Java Continue……

Chandima Jayamina
5 min readMay 27, 2021

Java 1.0, Java 1.1, Java 1.2, …………..

String support for Switch case.

Before java 1.7 or Java 7 switch cases does not strings. So we had to use Java primitive data types. But with the Java 1.7 we are able to use Strings inside switch cases.

It says better to use primitive data than strings, if the collection of data is strings only use as a best practice.

Try with resources

Before Java 1.7

We had to initialize all the resources and we were able to use them in try block. So after usage we had to manually close these objects. And the weakness is when the exceptions comes through both method we were not able to identify the root because we are not able to look at suppressed exceptions.

Java 1.7 Onward

So in here as we can see we are able to use resources in the try block and with auto closable interface we does not want to manually close the resources.

Auto closable Interface

If class implements Auto closable or closable interface then when we use try with resources, it will auto close. We don’t want to use finally block and manually close it.

Why we have to use Try With Resources

With try with resources the code is more readable and reduce the number of lines of code. it helps with automatic resource management. We don’t need to use finally block and close the resources. We can have multiple resources with try with resources by using semicolons as below code.

When using try with resources you should implement the resources to auto closable. This will help with you with better resource management.

Parallel Programing

Before java 1.8 any java program it uses only one core if there are more cores. This is a draw back which for huge programs.

But after java 1.8 we were able to use multiple cores in java programs. eg: list.parallelStream.filter();

In here we can see how it works. When we use one core it takes all the data as ne input and do the processing. If the data set is with 1 million data it will take huge time. But in parallel programing what happens is data set is divided to sets and use multiple cores and do the process parallelly.

Note

In the above example if we use parallelStream for a small data set it will take much time than normal filter what happens is it divide the data set to do the parallel filtering and after filtering it merge the results. Thats why for small data set it take much time than normal filtering so advise is to use the parallelStream filter only for large data sets. :)

Lambda Expression

Java is Object Oriented Programing :) not 100% true. With the time there are different programing languages came with functional programing. So with the time programmers used to go with functional programing. So with this risk java gave lambda expression for use functional programing in java.

This is the normal method we used in Java. Lets convert this to Lambda Expression.

Public — Access modifier (We can have non-access modifiers like static)

Void — Return type

print — Method Name

() — Parameters

In Lambda expressions we don’t have

  1. No access modifiers
  2. No return types
  3. No method names
  4. we have to add “— >” this after parameters.

So above method by convert it to lambda expression we can show as this.

If we have parameters

Compiler can sometimes guess the parameter arguments

If we have only single argument we can remove parenthesis also

If we have return we can remove the return also

So now we know how to move from normal method implementation to lambda expressions.

Note

For use of Lambda expressions we should have functional interface. Functional interface which we used to call as SAM interfaces, which has single abstract method. Functional interface can have multiple non abstract methods but should have one abstract method.

In here we can see we have only one abstract method, so this is a functional interface. As best practices we add annotation as @FunctionInterface above the interface, So other developers able to notify that this is SAM interface so they would not add more abstract methods.

Conclusion

In here we discussed the Strings for switch cases, Parallel Programing and Lambda expression and functional interfaces and when to use and how to use them in our day today programing life. So there are more awesome techniques java enables for us. So with next blogs. Hope this would add give advantage for you. Lets meet soon with the new features thanks for the time. :)

--

--