Java SE 17 (1Z0-829) flashcards
Java SE 17 (1Z0-829) · Build recall with a focused review session.
32 cards. Read the term and the definition here. The deck at the top of this page is optional.
- LocalDate vs LocalTime
- LocalDate holds a date without time or zone; LocalTime holds a time without date or zone. Both are immutable, so plusDays or plusHours return new instances and never mutate the original.
- Period vs Duration
- Period measures date-based amounts in years, months, and days. Duration measures time-based amounts in hours, minutes, seconds, and nanos. Applying Period to an Instant throws UnsupportedTemporalTypeException.
- DateTimeFormatter thread safety
- DateTimeFormatter is immutable and thread-safe, so one instance can be shared across threads. SimpleDateFormat is mutable and not thread-safe, requiring a new instance or synchronization per use.
- String text block rules
- A text block opens with triple quotes followed by a line break. Incidental indentation is stripped based on the least-indented line and closing delimiter. Escapes like backslash-s and backslash-triple-quote are supported.
- Integer cache range
- Integer.valueOf caches objects from -128 to 127. Comparing two boxed values in that range with == yields true; outside the range it yields false, so equals should be used for value comparison.
- Boolean.parseBoolean behavior
- Boolean.parseBoolean returns true only when the argument equals "true" ignoring case. Any other string, including null, returns false. The method never throws an exception for malformed input.
- LocalDate month-end adjustment
- Adding months to a LocalDate clamps the day to the last valid day. January 31 plus one month becomes February 29 in a leap year, not March 3, and the original object is unchanged.
- Immutable collection factories
- List.of, Set.of, and Map.of return unmodifiable collections that reject null elements. Mutation attempts throw UnsupportedOperationException. Set.of and Map.of also reject duplicate keys or elements at creation.
- Stream laziness
- Intermediate stream operations such as filter and map are lazy and run only when a terminal operation like collect or forEach executes. A stream cannot be reused after any terminal operation.
- Collectors.toMap duplicate keys
- Collectors.toMap throws IllegalStateException when two elements map to the same key unless a merge function is supplied. A null value also causes NullPointerException in the merge path.
- Optional orElse vs orElseGet
- orElse always evaluates its argument even when a value is present. orElseGet takes a Supplier and evaluates it only when the Optional is empty, avoiding unnecessary computation or side effects.
- HashMap null handling
- HashMap permits one null key and multiple null values. Hashtable and ConcurrentHashMap reject null keys and values. TreeMap rejects null keys because it relies on comparison ordering.
- Deque method pairs
- Deque offers offer, poll, and peek, which return special values on failure or emptiness. The add, remove, and element variants throw exceptions instead. ArrayDeque rejects null elements.
- Stream.toList mutability
- Stream.toList, added in Java 16, returns an unmodifiable list and rejects null elements. Collectors.toList returns a mutable ArrayList in practice, though the specification does not guarantee the type.
- Switch arrow form
- Arrow labels use case X -> and never fall through, so break is unnecessary. A block body returns a value with yield. The colon form falls through unless a break statement stops execution.
- Labeled break and continue
- A labeled break exits the labeled loop entirely. A labeled continue skips to the next iteration of the labeled loop. Labels must reference an enclosing loop for continue to compile.
- Switch expression exhaustiveness
- A switch expression must cover every possible value: all enum constants, all sealed subtypes, or a default branch. A switch statement used as a statement does not require exhaustiveness.
- Short-circuit operators
- The && and || operators skip evaluating the right operand when the result is already determined. The single-character & and | operators always evaluate both operands and are non-short-circuiting.
- Compound assignment implicit cast
- Compound assignment operators like += perform an implicit narrowing cast. byte b = 1; b += 1 compiles, while b = b + 1 fails because the arithmetic result is int.
- Pattern matching for instanceof
- Since Java 16, if (obj instanceof String s) declares s in the true branch without an explicit cast. The pattern variable is definitely assigned only where the type check is known to have succeeded.
- Unreachable code rule
- Code after return, throw, break, or continue in the same block is a compile error. A while(false) loop body is also flagged, but if(false) is permitted because the compiler does not treat it as constant.
- Checked vs unchecked exceptions
- Checked exceptions extend Exception but not RuntimeException and must be caught or declared. RuntimeException, Error, and their subclasses are unchecked and do not require handling by the compiler.
- Multi-catch restrictions
- A multi-catch clause joins alternatives with a vertical bar. The alternatives cannot be related by subclassing, and the catch parameter is implicitly final, so reassigning it is a compile error.
- try-with-resources closing order
- Resources declared in a try-with-resources are closed in reverse declaration order. Exceptions thrown during close are suppressed and attached to the primary exception, retrievable via getSuppressed.
- var restrictions
- var works only for local variables and lambda parameters with an initializer. It cannot be used for fields, method parameters, return types, catch parameters, or initialized with null.
- finally return override
- A return statement inside finally overrides any return or exception from the try and catch blocks. The original exception is discarded, which is why returning from finally is discouraged.
- Precise rethrow
- Since Java 7, rethrowing a caught exception can use a more precise static type when the try block can throw only that type. The compiler analyzes the possible exceptions rather than the declared catch type.
- Interface default method conflict
- When two interfaces provide conflicting default methods, the implementing class must override the method. It may delegate to a specific interface using InterfaceName.super.methodName() syntax.
- Functional interface definition
- A functional interface declares exactly one abstract method. Default and static methods do not count. The @FunctionalInterface annotation is optional but makes the compiler enforce the single-method rule.
- Method reference kinds
- Four kinds exist: static (Type::method), bound instance (obj::method), unbound instance (Type::method using the first parameter as receiver), and constructor (Type::new). The target type determines compatibility.
- Record generated members
- A record generates a canonical constructor, accessors named after components, and equals, hashCode, and toString. Records are implicitly final and cannot declare instance fields beyond their components.
- Sealed type permits clause
- A sealed class or interface restricts which types may extend or implement it. Permitted subtypes must be final, sealed, or non-sealed. The permits clause may be omitted when subtypes share the source file.