0 day streak
Code library

Intermediate Java exercises

Read these Intermediate Java snippets line by line — each one comes with a written breakdown of what the code does and why.

Java · Intermediate

Immutability with Records

Records are a special kind of class that acts as a transparent carrier for immutable data, automatically generating constructors, accessors, equals, and hashCode.

OOPSyntax
Open snippet →
Java · Intermediate

Automatic Resource Management

The try-with-resources statement ensures that each resource is closed at the end of the statement, preventing memory leaks and resource exhaustion.

Error HandlingBest Practices
Open snippet →
Java · Intermediate

Exception Chaining for Better Debugging

Exception chaining allows you to wrap a low-level exception (like IOException) into a higher-level business exception while preserving the original cause. This keeps the stack trace intact for debu…

Error HandlingBest Practices
Open snippet →
Java · Intermediate

Custom Annotations for Metadata

Custom annotations allow you to add metadata to your code that can be processed at runtime via Reflection. This is often used by frameworks for cross-cutting concerns like logging or validation.

OOPBest Practices
Open snippet →
Java · Intermediate

Memory Visibility with the volatile Keyword

The volatile keyword ensures that changes to a variable are always visible to all threads immediately. It prevents threads from caching the variable locally, ensuring they always read the latest va…

Async & ConcurrencyMemory Management
Open snippet →
Java · Intermediate

Generics: Upper Bounded Wildcards

Upper bounded wildcards (? extends T) allow a collection to hold objects of type T or any of its subclasses. This is useful for reading from a collection while maintaining type safety for various n…

OOPSyntax
Open snippet →
Java · Intermediate

The Strategy Pattern using Enums

In Java, Enums can have abstract methods that each constant must implement. This effectively implements the Strategy design pattern, allowing different behaviors to be associated with different enu…

OOPDesign Patterns
Open snippet →
Java · Intermediatespring

Testing Controllers with MockMvc

MockMvc allows you to test Spring MVC controllers without starting a full HTTP server. It provides a DSL for performing requests and verifying results.

TestingFrameworks
Open snippet →
Java · Intermediatespring

Custom Validation Constraint

Custom constraints allow you to extend Spring's validation capabilities by defining your own annotations. This is useful for complex business rules that aren't covered by standard JSR-303 annotatio…

FrameworksBest Practices
Open snippet →
Java · Intermediatespring

Transaction Propagation: REQUIRES_NEW

Propagation.REQUIRES_NEW creates a separate transaction that is independent of the caller's transaction. If the caller's transaction rolls back, this one will still commit, which is ideal for audit…

FrameworksDesign Patterns
Open snippet →
Java · Intermediatespring

Performance Optimization with @Cacheable

Spring Cache abstraction allows you to transparently cache expensive operations. Subsequent calls with the same ID will return the cached value instead of executing the method body.

PerformanceFrameworks
Open snippet →
Java · Intermediatespring

Conditional Bean Loading

Conditional beans allow your application to adapt its configuration based on environment variables or property files. This is essential for supporting different cloud providers or local development…

FrameworksDesign Patterns
Open snippet →
Java · Intermediatespring

Intercepting Requests with HandlerInterceptor

A HandlerInterceptor allows you to execute logic before or after a request reaches a controller. This is ideal for logging, authentication checks, or adding common headers without cluttering contro…

FrameworksDesign Patterns
Open snippet →
Java · Intermediatespring

Decoupling Components with Application Events

Application events facilitate loose coupling between components. Instead of calling an email service directly from a registration service, you publish an event that interested listeners can handle…

FrameworksDesign Patterns
Open snippet →
Java · Intermediatespring

Dynamic JSON Filtering using @JsonView

@JsonView allows you to control which fields of an object are serialized into JSON depending on the context. You can define marker interfaces to create 'views' (e.g., Public vs. Admin) and apply th…

FrameworksSyntax
Open snippet →