Six Important New Features in Java 8 (JDK 8)

Supun Kaluthanthri
4 min readJul 19, 2022

--

Java 8 Logo

Oracle released a new version of Java as Java 8 in March 18, 2014. It was a revolutionary release of the Java for software development platform. It includes various upgrades to the Java programming, JVM, Tools and libraries. I’m going to focus on the following features of Java 8.

  • Permanent Generation
  • Parallel Array Sorting
  • Base64 Encoding and Decoding
  • Date and Time API
  • Functional Interfaces
  • Lambda Expressions

If you want a complete list of features, feel free to check them out at OpenJDK.

Permanent Generation

What does this actually mean?

Who has never configured their “PremSize” or their “MaxPermSize” JVM memory?

This was normally done after receiving those ugly “java.lang.OutOfMemoryError: PermGen error” errors.

This has now been replaced by something called Metaspace. The Metaspace will re-size itself depending on the demand we have out of memory at runtime. If we need to, we can still tune the amount of Metaspace by setting the “MaxMetaspaceSize” param.

Check out this post on permanent generation for more details.

Parallel Array Sorting

We are used to soring arrays with “Arrays.sort”. This used Merge Sort or Tim Sort algorithms for the sorting. The problem they have is that they are executed sequentially, so we do not gain all the benefits multithreading has to offer us. For this purpose, they implemented “Arrays.parallelSort”.

When should we use this then?

We won’t gain direct benefits of using our 2 or more core processor right away.

The array should be of a certain size to see any gain in performance. Some comparisons say you will need around 2million elements in your array to start seeing improvements.

Check out this post on parallel array sorting for more detail on the benchmarking and how the algorithm works.

Base64 encoding and decoding

Not long ago I had to use Base64 encoding and decoding in a project to be able to transfer a file in JSON format.

I ended up using a 3rd party API (apache Base64 library) to do this .

This feature means we don’t have to search around for other implementations, making things a bit easier.

Some basic decoding and encoding examples

Encoding

String base64 = Base64.getEncoder().encodeToString("string to encode".getBytes("utf-8"));

Decoding

byte[] asBytes = Base64.getDecoder().decode("your base 64 string");

Date and time API

This is probably one of the features I am most excited about.

Dealing with dates and times in Java has always been a pain. We went from the now mostly deprecated methods in the Date class, remembering if Monday is represented as a 0 or a 1, the addition of the Calendar class to deal with time Zones and so on…

This made us use Joda Time or some other API to make things simpler. This won’t be necessary anymore.

The new API is very similar to Joda Time, so if you are already familiar with it you should catch things on the fly. If you never dealt with Joda Time do not worry, it is really simple to use.

Some date and time examples

javax.time.Clock

Clock.systemUTC(); //current time of your system in UTC.
Clock.millis();//time in milliseconds from 1/1/1970.

javax.tme.ZoneId

ZoneId zone = ZoneId.of(“Europe/London”);//zoneId from a timezone.
Clock clock = Clock.system(zone);//set the zone of a Clock.

javax.time.LocalDate

LocalDate date = LocalDate.now();//current date
String day = date.getDayOfMonth();//day of the month
String month = date.getMonthValue();//month
String year = date.getYear();//year

Functional Interfaces

A functional interface is the one that defines exactly one abstract method. We have for instance “java.lang.Runnable” defining the run abstract method:

public abstract void run();

We can still add as many default methods (non abstract) as we like.

While defining a new functional interface, we will have to define the new annotation “@FunctionalInterface”. This will allow us to block bad usages of functional interfaces as it will not compile if used improperly with the new annotation.

Lambda Expressions

Lambda expressions might be the biggest and most anticipated feature of Java 8. They are basically used to pass code instead of objects to a method or to deal with group of data to execute algorithms.

This will produce much simpler and more readable code. Lets take a look at some concepts around this.

Internal or External Interactions

The normal approach in Java is to iterate collections externally, so we would have something like this:

for (String value: myCollection) {
System.out.println(value);
}

What we are doing here is iterating the list and pulling objects one by one. It would be more natural if we could just say from the beginning what we want to extract form the collection. This is exactly the concept of how we would do it with lambdas:

myCollection.forEach((String value) -> System.out.println(value));

Passing Behaviours

This is for me where we can really find potential in lambda expressions. We can pass behaviours to functions making basic functionality much more generic and greatly increasing usability in our project.

For instance, if we want to create a method that prints all the elements of a List<String> we would do the following:

public void printAllStrings (List listString) {
for(String stringObject : listString) {
System.out.println(stringObject);
}
}

If we now wanted to print only the strings that are longer than 4 characters long:

public void printLongerThan4Strings (List listString) {
for(String stringObject : listString) {
if(stringObject.length() > 4) {
System.out.println(stringObject);
}
}
}

In Java 8 we can make this process more generic and reuse more of our code. We can pass a predicate to do the filtering. It would look something like this:

public void printAllStrings (List listString, Predicate p) {
for(String stringObject : listString) {
if(p.test(stringObject)) {
System.out.println(stringObject);
}
}
}

Now all we have to do is pass the predicate in the call:

printAllStrings(listString, s -> true);
printAllStrings(listString, s -> s.length() > 4);

Clean, fast and reusable.

Feel free to provide productive feedback and comments. I will be happy to update the exercises accordingly.

References: JDK 8 Features | Permanent Generation | Parallel Array Sorting

--

--