eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

In this tutorial, we’ll have a quick look at some of the most interesting new features in Java 8.

We’ll talk about interface default and static methods, method reference and Optional.

We have already covered some the features of the Java 8 release — stream API, lambda expressions and functional interfaces — as they’re comprehensive topics that deserve a separate look.

2. Interface Default and Static Methods

Before Java 8, interfaces could have only public abstract methods. It was not possible to add new functionality to the existing interface without forcing all implementing classes to create an implementation of the new methods, nor was it possible to create interface methods with an implementation.

Starting with Java 8, interfaces can have static and default methods that, despite being declared in an interface, have a defined behavior.

2.1. Static Method

Consider this method of the interface (let’s call this interface Vehicle):

static String producer() {
    return "N&F Vehicles";
}

The static producer() method is available only through and inside of an interface. It can’t be overridden by an implementing class.

To call it outside the interface, the standard approach for static method call should be used:

String producer = Vehicle.producer();

2.2. Default Method

Default methods are declared using the new default keyword. These are accessible through the instance of the implementing class and can be overridden.

Let’s add a default method to our Vehicle interface, which will also make a call to the static method of this interface:

default String getOverview() {
    return "ATV made by " + producer();
}

Assume that this interface is implemented by the class VehicleImpl.

For executing the default method, an instance of this class should be created:

Vehicle vehicle = new VehicleImpl();
String overview = vehicle.getOverview();

3. Method References

Method reference can be used as a shorter and more readable alternative for a lambda expression that only calls an existing method. There are four variants of method references.

3.1. Reference to a Static Method

The reference to a static method holds the syntax ContainingClass::methodName.

We’ll try to count all empty strings in the List<String> with the help of Stream API:

boolean isReal = list.stream().anyMatch(u -> User.isRealUser(u));

Let’s take a closer look at lambda expression in the anyMatch() method. It just makes a call to a static method isRealUser(User user) of the User class.

So, it can be substituted with a reference to a static method:

boolean isReal = list.stream().anyMatch(User::isRealUser);

This type of code looks much more informative.

3.2. Reference to an Instance Method

The reference to an instance method holds the syntax containingInstance::methodName.

The following code calls method isLegalName(String string) of type User, which validates an input parameter:

User user = new User();
boolean isLegalName = list.stream().anyMatch(User::isLegalName);

3.3. Reference to an Instance Method of an Object of a Particular Type

This reference method takes the syntax ContainingType::methodName.

Let’s look at an example:

long count = list.stream().filter(String::isEmpty).count();

3.4. Reference to a Constructor

A reference to a constructor takes the syntax ClassName::new.

As constructor in Java is a special method, method reference could be applied to it too, with the help of new as a method name:

Stream<User> stream = list.stream().map(User::new);

4. Optional<T>

Before Java 8, developers had to carefully validate values they referred to because of the possibility of throwing the NullPointerException (NPE). All these checks demanded a pretty annoying and error-prone boilerplate code.

Java 8 Optional<T> class can help to handle situations where there is a possibility of getting the NPE. It works as a container for the object of type T. It can return a value of this object if this value is not a null. When the value inside this container is null, it allows doing some predefined actions instead of throwing NPE.

4.1. Creation of the Optional<T>

An instance of the Optional class can be created with the help of its static methods.

Let’s look at how to return an empty Optional:

Optional<String> optional = Optional.empty();

Next, we return an Optional that contains a non-null value:

String str = "value";
Optional<String> optional = Optional.of(str);

Finally, here’s how to return an Optional with a specific value or an empty Optional if the parameter is null:

Optional<String> optional = Optional.ofNullable(getString());

4.2. Optional<T> Usage

Let’s say we expect to get a List<String>, and in the case of null, we want to substitute it with a new instance of an ArrayList<String>.

With pre-Java 8’s code, we need to do something like this:

List<String> list = getList();
List<String> listOpt = list != null ? list : new ArrayList<>();

With Java 8, the same functionality can be achieved with a much shorter code:

List<String> listOpt = getList().orElseGet(() -> new ArrayList<>());

There is even more boilerplate code when we need to reach some object’s field in the old way.

Assume we have an object of type User that has a field of type Address with a field street of type String, and we need to return a value of the street field if some exist or a default value if street is null:

User user = getUser();
if (user != null) {
    Address address = user.getAddress();
    if (address != null) {
        String street = address.getStreet();
        if (street != null) {
            return street;
        }
    }
}
return "not specified";

This can be simplified with Optional:

Optional<User> user = Optional.ofNullable(getUser());
String result = user
  .map(User::getAddress)
  .map(Address::getStreet)
  .orElse("not specified");

In this example, we used the map() method to convert results of calling the getAdress() to the Optional<Address> and getStreet() to Optional<String>. If any of these methods returned null, the map() method would return an empty Optional.

Now imagine that our getters return Optional<T>.

In this case, we should use the flatMap() method instead of the map():

Optional<OptionalUser> optionalUser = Optional.ofNullable(getOptionalUser());
String result = optionalUser
  .flatMap(OptionalUser::getAddress)
  .flatMap(OptionalAddress::getStreet)
  .orElse("not specified");

Another use case of Optional is changing NPE with another exception.

So, as we did previously, let’s try to do this in pre-Java 8’s style:

String value = null;
String result = "";
try {
    result = value.toUpperCase();
} catch (NullPointerException exception) {
    throw new CustomException();
}

And the answer is more readable and simpler if we use Optional<String>:

String value = null;
Optional<String> valueOpt = Optional.ofNullable(value);
String result = valueOpt.orElseThrow(CustomException::new).toUpperCase();

Notice that how to use Optional in our app and for what purpose is a serious and controversial design decision, and explanation of all its pros and cons is out of the scope of this article. But there are plenty of interesting articles devoted to this problem. This one and this one could be very helpful to dig deeper.

5. Conclusion

In this article, we briefly discussed some interesting new features in Java 8.

There are of course many other additions and improvements spread across many Java 8 JDK packages and classes.

But the information illustrated in this article is a good starting point for exploring and learning about some of these new features.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)