If you’re already familiar and well-versed with basic Java syntax, object-oriented principles, and standard libraries, it’s time to move on to advanced Java. Java has a treasure trove of powerful features that can drastically improve the quality, performance, and readability of your codebase. Knowing these will make your code cleaner, performant, and more professional.
In this article, we’ll walk through advanced Java concepts that will help you in writing robust, modern Java.
1. Functional Interfaces and Custom Lambdas
Java 8 introduced lambdas, but many developers only use them with built-in types like Predicate or Function. You can define custom functional interfaces to make your code cleaner and more expressive. Let us define a custom functional interface and explore this concept in detail.
Before functional interfaces were available, we were forced to create classes to encapsulate even a single piece of functionality. This resulted in a lot of boilerplate code, which makes the code less intuitive and also makes the code complex.
What is a functional interface?
Any interface that defines a single abstract method (SAM )is functional. It is a good idea to annotate the interface with @FunctionalInterface
To explain the concept, we’ll use a simple function, converts a given string to uppercase. Of course, this function is already available in the String class. But our goal is just to demonstrate the functional interface, hence this is okay.
Functional Interface: Example
package com.thestembook;
public class FunctionInterfaceDemo {
@FunctionalInterface
interface UpperCase<T> {
T execute(String data) throws Exception;
}
public static <T> T format(String s,UpperCase<T> task) throws Exception {
return task.execute(s);
}
public static void main(String[] args) throws Exception {
String output = FunctionInterfaceDemo.format("Hello World",x -> x.toUpperCase());
System.out.println(output);
}
}
Code language: PHP (php)
We have created a functional interface and also shown how to pass an implementation of the interface when using it in a lambda. This is shown in the FunctionInterfaceDemo.format method call. You can create more complex interfaces that takes many parameters.
Sealed Classes and Pattern Matching (Java 17+)
With the introduction of sealed classes, Java is moving closer to pattern matching and algebraic data types — a feature long appreciated in functional languages. Sealed classes and interfaces restrict which other classes or interfaces can extend or implement them. This is a way of restricting the maximum implementations or subclassing of a given type. Let us look at an example.
Sealed Interace : Example
package com.thestembook;
public class SealedClassDemo {
sealed interface Player permits VideoPlayer, MusicPlayer {
void play();
}
public static final class VideoPlayer implements Player {
public VideoPlayer() { }
@Override
public void play() {
System.out.println("playing video");
}
}
public static final class MusicPlayer implements Player {
public MusicPlayer() { }
@Override
public void play() {
System.out.println("playing music");
}
}
public void start(Player player) {
player.play();
}
public static void main(String[] args){
SealedClassDemo sealedClassDemo = new SealedClassDemo();
sealedClassDemo.start(new MusicPlayer());
}
}
Code language: PHP (php)
The start() method accepts a Player interface, and it can safely assume that the supplied implementations will be either a VideoPlayer or a MusicPlayer. This gives you exhaustive checking and reduces bugs from unhandled types.
Clean Resource Management with try-with-resources
You probably might be using try-with-resources for I/O streams. But it can be used for any AutoCloseable, including custom objects. Let us explore this with an example
Example: try-with-resources , autocloseable
package com.thestembook;
public class TryWithResources {
static class Player implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("close method ...");
}
}
public static void main(String[] args) throws Exception {
try(Player player = new Player()) {
System.out.println("running ..");
}
}
}
Code language: PHP (php)
Output
> Task :com.thestembook.TryWithResources.main()
running ..
Code language: CSS (css)
I will be updating this post regularly with more useful features. Keep coming back to get the latest updates.