Pages

Topics

Tools

↑↓navigate↵openescclose

Java guides

Every published Java guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Java guides

A Java Optional container being unwrapped with orElseThrow, showing a value being returned or an exception being raised.
Java

Java Optional orElseThrow: Usage and Examples

Learn how to use Optional.orElseThrow() to unwrap values, throw custom exceptions, and avoid the pitfalls of get(), orElse(), and orElseGet().

JavaOptionalException HandlingFunctional Programming
Diagram showing Java Optional orElseGet lazy evaluation versus eager orElse
Java

Java Optional orElseGet: Lazy Default Values Explained

Java Optional.orElseGet supplies a default value lazily. See how it differs from orElse, when to use it, and how to avoid unnecessary work and side effects.

JavaOptionalorElseGetLazy Evaluation
Illustration of Java Optional orElse showing a default value being selected when the Optional is empty.
Java

Java Optional orElse: Usage and Pitfalls

Learn how to use Java Optional orElse correctly, including its eager evaluation, differences from orElseGet, and common pitfalls.

Java OptionalorElseorElseGetNull Handling
Illustration of Java Optional.get() showing a value inside an Optional container and a warning about NoSuchElementException.
Java

Java Optional.get(): Usage, Risks, and Safer Alternatives

Learn how Java Optional.get() works, why it throws NoSuchElementException, and safer alternatives like orElse, orElseGet, orElseThrow, and map.

OptionalJavaNoSuchElementExceptionorElseThrow
A Java Optional box with two branches: one for a present value and one for an empty fallback, illustrating ifPresentOrElse.
Java

Java Optional ifPresentOrElse Explained

Learn how to use Optional.ifPresentOrElse to handle present and absent values in a single functional expression, with syntax, examples, and edge cases.

OptionalJava 9ifPresentOrElsenull handling
Diagram showing Java Optional.ifPresent() applying a consumer to a present value, with a null path leading to no operation.
Java

Java Optional ifPresent: Usage and Pitfalls

Learn how to use Java Optional.ifPresent() correctly, manage side effects, avoid common mistakes, and decide when it is a better choice than traditional null checks.

JavaOptionalifPresentNull Safety
Illustration of Java Optional.isEmpty() method showing an empty box versus a box with a value.
Java

java optional isempty: Using Optional.isEmpty() in Java

java optional isempty: Learn how to use Optional.isEmpty() in Java 11+ to check for absent values, compare it with isPresent(), and avoid common pitfalls.

OptionalJava 11isEmptyJava Streams
Illustration of a Java Optional container with a checkmark for presence and a warning sign about using get() directly, representing safe handling choices.
Java

Java Optional isPresent: Usage and Pitfalls

Understand when Java's Optional.isPresent() is appropriate, why pairing it with get() is an anti-pattern, and how ifPresent(), orElse(), and map() make code safer and more concise.

Java OptionalNull SafetyFunctional ProgrammingException Handling
A Java Optional container with an empty slot, symbolizing the absence of a value
Java

Java Optional Empty: How to Check and Handle

java optional empty: Learn how to detect and handle empty Optional instances in Java, including isPresent(), orElse, and orElseGet patterns.

OptionalNull SafetyJava 8Functional Programming
A diagram showing Optional.ofNullable wrapping a null value into an empty Optional and a non-null value into a populated Optional.
Java

Using Optional.ofNullable to Handle Null in Java

How Optional.ofNullable works, how it differs from Optional.of, and practical examples for handling nullable values in Java.

Java OptionalNull HandlingofNullableJava 8