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.

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

Regression testing is an important step in the release process, to ensure that new code doesn't break the existing functionality. As the codebase evolves, we want to run these tests frequently to help catch any issues early on.

The best way to ensure these tests run frequently on an automated basis is, of course, to include them in the CI/CD pipeline. This way, the regression tests will execute automatically whenever we commit code to the repository.

In this tutorial, we'll see how to create regression tests using Selenium, and then include them in our pipeline using GitHub Actions:, to be run on the LambdaTest cloud grid:

>> How to Run Selenium Regression Tests With GitHub Actions

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

1. Introduction

When we work with wildcards in generics in Java, we sometimes see a compile-time error that contains the phrase “capture of ?”.

In this article, we’ll explore what a “capture” is, in which cases we see such an error message, and how we can solve the problem.

2. What Is a Capture?

2.1. Parameterized Lists

Let’s define a method that takes a list of Numbers as a parameter:

void updateNumbers(List<Number> numbers) {
}

As the type Integer extends Number, we might want to call this method with a list of Integers:

List<Integer> integers = Arrays.asList(1, 2, 3);
updateNumbers(integers);

This code doesn’t compile because even though Integer extends Number, the parameterized list List<Integer> does not extend List<Number>. Therefore, we’ll get a compile-time error:

java: incompatible types: java.util.List<java.lang.Integer> cannot be converted to java.util.List<java.lang.Number>

2.2. Wildcards

To be able to pass a list of any type that extends Number to our method, we can change the method signature to:

void updateNumbers(List<? extends Number> numbers) {
}

This seems to work fine. However, if we want to update one of the elements of the list inside our method:

numbers.set(0, Integer.getInteger("1"));

We’ll get another compile-time error:

java: incompatible types: java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number

Let’s understand this error message. The compiler attempts to convert the type “java.lang.Integer” to a type “capture#1 of ? extends java.lang.Number”.

Integer is the type of the element that we pass to the method, so that part of the message is straightforward. We have, however, not defined the second type anywhere ourselves. That’s where the Java compiler comes in. During the compilation process, the Java compiler creates a new, temporary type for us that represents the generic parameter with the wildcard. 

We can see even better what happens when we call set() several times:

numbers.set(0, Integer.getInteger("1"));
numbers.set(1, Long.valueOf(20));
numbers.set(2, 30);

Here, the compiler produces the following error messages:

java: incompatible types: java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number
java: incompatible types: java.lang.Long cannot be converted to capture#2 of ? extends java.lang.Number
java: incompatible types: int cannot be converted to capture#3 of ? extends java.lang.Number

We see that for every call to the set() method, the compiler has created a dedicated, internal type with a counter (note the #1, #2, and #3) as part of the name.

Simply put, a capture is an internal type that the compiler creates to represent a generic type that uses a wildcard.

In the following sections, we’ll look at the reasons for this behavior and how we can solve the compilation error.

3. Type Safety

3.1. Raw Lists and Casting

The reason for the compile-time error we saw is type safety. Generics were introduced to guarantee type-safety at compile type. If we define a raw list:

List numbers = new ArrayList();

We can add elements of any type to that list:

numbers.add("a string");

As a result, the compiler cannot know which types the list actually contains and can only assume all elements are of type Object. Therefore, we can’t call any method that belongs to the class Integer on elements of our list:

List numbers = new ArrayList();
numbers.add(1);
numbers.add(2);
numbers.add(3);

int integer = numbers.get(0); // compilation error

We can get the code to compile using a cast:

int integer = (int) numbers.get(0);

However, a cast isn’t type-safe, as we cannot be sure that the list contains only integers.

3.2. Using Wildcards

Let’s revisit our example where we pass a list of integers to the update method:

List<Integer> integers = Arrays.asList(1, 2, 3);
updateNumbers(integers);

And the method:

void updateNumbers(List<? extends Number> numbers) {
    numbers.set(0, Double.valueOf(1.2));
    numbers.set(1, Long.valueOf(20));
}

Here, we pass a list of Integers to a method that accepts a list of anything that extends Number. That’s fine. However, we cannot add values of type Double or Long.

The compiler doesn’t allow us to add an element of any type to the list; within the method, it only knows that it’s a list of elements that extend number, but doesn’t know the exact type.

3.3. Solving the Error Using Type Parameters

We can solve the problem using type parameters:

public <T extends Number> void updateNumbers(List<T> numbers, T element, int index) {
    numbers.set(index, element);
}

Now, the compiler knows that the list is of one specific type T which extends Number, and that the type of the element is the same as the type of the list.

4. Arrays and Type Safety

We can better understand why type-safety at compile time is an important feature by looking at the following method that takes an array as input:

public static void updateArrayOfNumbers(Number[] numbers) {
    numbers[0] = 10;
    numbers[1] = 2.3;
}

Unlike the generic version of this method, the code compiles. However, if we call the method:

methodWithArray(new Integer[]{1, 2, 3});

We’ll get an ArrayStoreException exception at runtime:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double

This example shows why the Java compiler is very strict when using wildcards with generics.

5. More Wildcards

In Java, we can define unbounded wildcards, upper-bounded wildcards, and lower-bounded wildcards:

List<?> unboundedList;
List<? extends Number> upperBoundedList;
List<? super Number> lowerBoundedList;

In this article, we’ve only covered upper-bounded wildcards, however, the same compile-time error can occur with unbounded and lower-bounded wildcards as well.

6. Conclusion

A compiler error message that contains the phrase “capture of ?” is best understood if we understand the underlying concepts of generics in Java.

In this article, we’ve learned that such an error message occurs when the compiler cannot guarantee type safety when we use wildcards. We also saw how we can solve the problem using type parameters.

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)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments