var — local variable type inference
`var` lets the compiler infer a local variable's type from its initializer. The type remains static and does not change with later assignments. In Java SE 21, `var` is a contextual keyword, not a type name.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
`var` lets the compiler infer a local variable's type from its initializer. The type remains static and does not change with later assignments. In Java SE 21, `var` is a contextual keyword, not a type name.
volatile is a reserved keyword marking a field used for communication between threads. It provides visibility and happens-before ordering guarantees, but does not provide mutual exclusion or make operations such as count++ atomic.
In Java SE 21, the contextual keyword when introduces an additional condition (a guard) after a case pattern in a switch statement or expression. The branch is selected only when the value matches the pattern and the guard evaluates to true.
The while keyword introduces a loop that checks its condition before each execution of its body. The body may never execute; the condition must have type boolean or Boolean.
In Java SE 21, `with` is a contextual keyword in the `provides` directive of a module declaration. It separates the service type from its provider list; outside that context, it can be an identifier.
The yield statement supplies a value to the nearest enclosing switch expression. It is useful in a block arm that executes several statements before producing its result. In Java SE 21, yield is a contextual keyword.
The null literal denotes a reference that does not point to any object. It has the special null type and can be assigned to variables of reference types, but not to variables of primitive types.
main: practical reference: The Java launcher invokes a public static void main(String[] args) method on the selected class after loading, linking, and class initialization.
`close()` releases an object's resources. A try-with-resources statement calls it automatically, but the interface does not require a repeated call to have no effect.
Use `getSimpleName()` for a short type label in a report. For a nested type, the result leaves out both the package and the enclosing type names.
`Collection.size()` returns an element count as an `int`. The expression `repo.all().size()` counts the elements in the collection returned by `all()`.
Store changes in a `Deque` and remove the newest one first. Check that the history has an entry before each call to `pop()`.
`Iterable.iterator()` returns an iterator. Support for successive passes does not guarantee that overlapping passes are independent or that each call returns a new iterator object.
Check whether an iterator has another element, retrieve it, and optionally remove it. Distinguish iterator exhaustion, an invalid call sequence, and unsupported removal.
`List.add(E)` appends one element to the existing list and returns `true` if the call completes normally. Not every list supports adding elements.
Check whether a list holds an element equal to a given value before adding a task name or marking the task complete.
Remove the first occurrence of a value, such as a subscriber name, and use the `boolean` result to tell whether anything was removed. Contrast this with removal by index.
Find the absolute difference between two `double` values and check whether it falls within a tolerance, including at the boundary.
Define Person equality by name and age, distinguish it from reference identity, and implement a matching hashCode method.
Objects that are equal according to `equals()` must have the same hash code. The reverse does not hold: matching hash codes do not prove equality.
Use `Objects.equals(a, b)` to compare objects according to their `equals` rules, even when one or both arguments may be `null`.
`Objects.hash` combines values into one `int` hash code. Learn how `Object...` handles mixed arguments, when boxing occurs, and how to stay consistent with `equals`.
Check a required parameter: `Objects.requireNonNull` returns the same reference if it isn't `null` and normally throws `NullPointerException` if it is. You can supply a custom message.
Give an object a readable description and see how `println(Object)` prints it without an explicit call to `toString()`.