Spring / Spring Boot 4 Basics Interview Questions
What are JSpecify nullability annotations in Spring Boot 4 and why are they important?
Spring Boot 4 adopts JSpecify annotations (@Nullable, @NonNull, @NullMarked) portfolio-wide, deprecating Spring's own org.springframework.lang annotations. JSpecify is a collaborative standard backed by Google, JetBrains, Broadcom (Spring), Uber, Oracle, and OpenJDK.
| Annotation | Package | Meaning |
|---|---|---|
| @NonNull | org.jspecify.annotations | The element is never null; tooling enforces this contract |
| @Nullable | org.jspecify.annotations | The element may be null; callers must handle null |
| @NullMarked | org.jspecify.annotations | Applied at package/class level: all unannotated types are non-null by default |
| @NullUnmarked | org.jspecify.annotations | Opts a scope back out of null-marked assumptions |
// package-info.java: declare the whole package as null-safe @NullMarked package com.example.service; import org.jspecify.annotations.NullMarked; // Service class: unannotated types are non-null by default @Service public class OrderService { // Non-null by default (from @NullMarked) public Order createOrder(String productId, int quantity) { return new Order(productId, quantity); } // Explicitly mark where null is possible: public @Nullable Order findOrder(@Nullable String orderId) { if (orderId == null) return null; return orderRepository.findById(orderId).orElse(null); } } // Maven dependency: <dependency> <groupId>org.jspecify</groupId> <artifactId>jspecify</artifactId> <!-- Version managed by Spring Boot 4 BOM --> </dependency> // Kotlin benefit: JSpecify annotations are automatically // translated into Kotlin nullability by Kotlin 2.2 compiler // val order: Order? = service.findOrder(null) <- correct type // val order: Order = service.createOrder("p1", 2) <- non-null inferred
Tool support: IntelliJ IDEA 2025.3 ships first-class JSpecify support with full data-flow analysis. Build-time null checking via NullAway (requires JDK 21+) catches nullability violations at compile time. Previously fragmented null annotation libraries (Spring's own, JetBrains', FindBugs) are being consolidated around JSpecify.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
