ZonedDateTime: Same instant or same wall-clock time?
In Java 21, create a zoned date-time, explicitly select either occurrence of a repeated local time, and change zones without confusing the instant with the clock reading.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
In Java 21, create a zoned date-time, explicitly select either occurrence of a repeated local time, and change zones without confusing the instant with the clock reading.
Use `ZoneId.of` to select a regional time zone or fixed UTC offset and display the same instant in Warsaw, New York, and Tokyo.
Look up the UTC offsets in Warsaw and New York at the same instant in 2025, and see why a fixed offset cannot replace a regional time zone.
ZoneOffsetTransition describes a change in UTC offset. The isGap() method identifies a gap, while getDateTimeAfter() returns the local date-time at the transition boundary using the new offset.
ZoneRules describes changes to a zone's offset. Its getValidOffsets and getTransition methods detect nonexistent or ambiguous local times before an offset is selected.
Java program architecture: Java architecture uses package and JPMS module boundaries: module-info.java declares requires, exports, opens, uses, and provides, while the JVM loads compiled bytecode. A fluent builder with a recursive self type, such as B extends Builder<B>, preserves the concrete builder type across chained methods and inheritance.
Asynchrony and concurrency in Java: Java 21 concurrency includes Thread, ExecutorService, Future, CompletableFuture, and virtual threads, while the Java Memory Model defines visibility through happens-before relations. The Java Memory Model (JMM) defines cross-thread reads; for ordinary mutable or non-final state, safe publication requires happens-before from completed initialization to the reader.
Boxing converts a primitive value to a wrapper object, while unboxing retrieves its value. Learn how == differs from equals and why null and shared wrapper instances can cause surprises.
Java collections: The Java Collections Framework separates Collection, List, Set, Queue, and Map interfaces from implementations, with equality, ordering, and mutability contracts determined by the type.
Comments and documentation in Java: Java recognizes line //, block /* ... */, and documentation /** ... */ comments; javadoc interprets tags and generates API documentation from doc comments.
Conditional control flow in Java: Java selects control flow with boolean if/else or a switch statement or expression, which in Java 21 supports type and record patterns, when guards, and case null.
Data pipelines in Java: A Java data pipeline commonly combines a source, lazy Stream intermediate operations, and one terminal operation; elements are pulled only as needed.
A `final` field does not freeze an array. Copying mutable data on both input and output keeps shared references from changing an object's state.
Copy a list in the constructor and in `getRoles()` so callers cannot modify the list stored in a DTO. A `final` field alone does not provide that protection.
Separate data sent across a boundary from domain state and behavior. Map five fields explicitly to create independent snapshots that never carry `passwordHash`.
Exceptions and error handling in Java: Java reports failures as Throwable: Error normally represents JVM-level problems, Exception covers application conditions, and RuntimeException is unchecked.
Expressions and operators in Java: Java expressions evaluate left to right under fixed precedence, numeric promotion, and arithmetic, logical, bitwise, comparison, and assignment operators.
Filtering and transformations in Java: Filtering keeps elements satisfying a Predicate, map transforms each element, and flatMap expands each result into zero or more values in Stream or Optional.
Finite-state machines in Java: A finite-state machine models state and event as enums or sealed types, with a transition function selecting the next state, often through an exhaustive switch expression.
A generic self type keeps subclass-specific methods available after inherited fluent calls. The terminal `build()` call creates a separate product from the builder’s current state.
Functions and functional interfaces in Java: Java represents behavior with methods and with lambdas or method references targeted to a functional interface having one abstract method.
Java can infer a generic method’s type arguments from the call arguments and, where applicable, the expected result type. Boxing lets an `int` argument take part in such a call as an `Integer`, not as a primitive type argument.
Generics in Java: Generics provide compile-time checking, while type erasure removes most type arguments and inserts casts or bridge methods; generic method inference derives arguments from parameters and the target type.
An `Object[]` alias lets a `List<Integer>` replace a `List<String>` in a varargs array. The store succeeds, and no exception occurs unless the element is read as a `String`.