paint-brush
Is Migrating From Java 6 to Java 8 Worth It?by@sergeidzeboev
720 reads
720 reads

Is Migrating From Java 6 to Java 8 Worth It?

by Sergei DzeboevJune 14th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Java 8 was released almost 10 years ago, but some projects are still using the previous version of Java. I have seen projects that were using older versions of Java and surrounding libraries that people don’t want to update just because it works. I gathered the main changes in LTS Java versions that could convince the teams I worked with to migrate.
featured image - Is Migrating From Java 6 to Java 8 Worth It?
Sergei Dzeboev HackerNoon profile picture


As a Java developer, I went through many migrations whether it was library, framework, or versions of the programming language itself. I have seen projects that were using older versions of Java and surrounding libraries that people don’t want to update just because it works and nobody knows what will happen if somebody updates something. I have decided to prepare a series of articles where I gathered the main changes in LTS Java versions that could convince the teams I worked with to migrate. In this particular article, I will be focusing on changes that were done from Java 6 to Java 8.


Java 6 → Java 8

I guess Java 8 was one of the memorable major versions of Java that everybody was waiting for, and even though it was released almost 10 years ago there are projects that are still using the previous version of Java. From a variety of changes here are the main ones that could convince you to do the migration:


Lambda Expressions

If you ask any Java developer about transitioning from Java 6 to 8 the first thing that you will probably hear is lambda expressions. Indeed lambda expressions are the most noticeable milestone in Java. Lamba expressions not only could make your code more concise allowing you not to use anonymous classes anymore but also became a starting point for implementing functional interfaces and method references. Let’s compare the code:


Java 6. Anonymous class:

Comparator<Integer> intComparator = new Comparator<Integer>() {
	@Override
	public int compare(Integer i1, Integer i2) {
		return i1.compareTo(i2);
	}
};


Java 8. Lamba expression:

Comparator<Integer> intComparator = (Integer i1, Integer i2) -> i1.compareTo(i2);


So, in this particular example, lambda expressions reduced the number of lines from 6 to 1. This change can drastically reduce the lines of code in real projects; this leads to more concise and readable code which, no doubt, makes it easier to support. Using method reference you can make your code even more concise:


Java 8. Method reference:

Comparator<Integer> intComparator = Integer::compareTo;


Date And Time API

Java 8 introduced a new date and time API that provides a more robust and flexible way to work with dates and times. The previous version of API without exaggeration could be called inconvenient; it wasn’t possible to get year or month without additional calculations, this is why a lot of developers were using additional libraries to work with dates:


Java 6. Date:

Date today = new Date();
int currentYear = today.getYear()+1900;
int currentMonth = today.getMonth()+1;


As you can see you could not just get a year or month from a date, you had to make further additions in order to get the correct data.


Java 8. LocalDate:

LocalDate today = LocalDate.now();
int year = today.getYear();
Month month = today.getMonth();


With Java 8 you don’t have to do any additional operations, and you always get the correct data. Of course, the code above is just a demonstrative example among others, and no wonder that developers were using other libraries.


Default Methods

Another useful innovation in Java 8 is the default methods for interfaces. Default methods allow adding default functionality for interfaces that makes the difference between interfaces and abstract classes almost disappear. It was done for a reason. Officially default methods were added to keep compatibility with the older code. Thus, they can be used to add default functionality to any class that implements an interface with a default method:

public interface SummingInterface {

        default int sumOfLowestAndHighest() {
            return takeHighestValue() + takeLowestValue();
        }

        int takeLowestValue();
        int takeHighestValue();
    }


Another appliance is to provide a more concise method name, It works well when it comes to using wordy JPA methods:

public interface JpaInterface {

	default List<FooBarRecord> findValuesBetween(int foo, int bar) {
		return findByFooInAndBarWhereBarLessOrEqualsAndFooGreaterOrEquals(foo, bar);

	}


Stream API

The final attractive feature in Java 8 is Stream API. The API brings a functional programming style to Java when it comes to manipulating collections. It makes operations such as filtering, mapping, and collecting into a new collection very easy to do, which in return makes code more robust, concise, and bug-free. Let’s consider a simple example of finding the lowest value but not larger than 100:


Java 6. Classic traversal:

Integer result = Integer.MIN_VALUE;
for (Integer value : integers) {
	if (value < 100 && value > result) {
		result = value;
	}
}
return result;


Java 8. Stream API:

return integers.stream()
		.filter(integer -> integer < 100)
		.max(Integer::compareTo)
		.orElse(Integer.MIN_VALUE);


Again, the code in Java 8 is more concise and much easier to read and understand what actually happens there.



Verdict

Java 8 in comparison with the previous versions made a big step forward making coding in Java much more convenient, so if for any reason you’re still using a version below, the first thing that you probably want to do is to migrate to Java 8. Java’s backward compatibility will allow you to do it without any issues, so do not hesitate to transition. However, there could be one issue when migrating to Java 8, and that is not being familiar with the new constructs of the language, which can be easily overcome.


The next coming article will be devoted to the main changes between Java 8 and Java 11, and again it will be focused only on the changes that can affect migration.