Java SE 11 Developer flashcards
Java SE 11 Developer · Build recall with a focused review session.
36 cards. Read the term and the definition here. The deck at the top of this page is optional.
- Primitive widening order
- Java widens byte to short to int to long to float to double without a cast. char widens to int and above, but not to short. Narrowing in the reverse direction always requires an explicit cast.
- Integer cache range
- Integer.valueOf caches values from -128 to 127 by default. Two autoboxed Integers in that range compare equal with ==, but values outside it create distinct objects, so equals is required.
- Definite assignment rule
- A local variable must be assigned on every path before it is read, or compilation fails. Instance and static fields instead receive default values such as 0, false, or null.
- var restrictions
- var works only for local variables with an initializer, including for-loop indices. It cannot be used for fields, method parameters, or return types, and it does not make the variable dynamically typed.
- switch supported types
- switch accepts byte, short, char, int, their wrappers, String, and enum constants. It rejects long, float, double, and boolean. Case labels must be compile-time constants and must be unique.
- Labeled break and continue
- A label before a loop lets break label exit that outer loop and continue label skip to its next iteration. An unlabeled break exits only the innermost loop or switch statement.
- Short-circuit evaluation
- The && and || operators skip the right operand when the left already determines the result. The non-short-circuit & and | always evaluate both operands, even for boolean expressions.
- do-while guarantee
- A do-while loop executes its body at least once because the condition is tested after the body. A plain while loop may execute zero times if the condition is initially false.
- Interface default methods
- A default method in an interface supplies a body so implementing classes need not override it. If two interfaces provide conflicting defaults, the class must override and resolve the conflict explicitly.
- Covariant return type
- An overriding method may declare a return type that is a subtype of the overridden method's return type. The parameter list must match exactly, and return type alone cannot distinguish overloads.
- Static method hiding
- A static method in a subclass with the same signature hides the superclass static method rather than overriding it. The version invoked depends on the reference type, not the object type.
- Protected access scope
- protected members are visible within the same package and to subclasses in other packages. A subclass outside the package can access them only through a reference of its own type.
- Checked versus unchecked
- Checked exceptions extend Exception but not RuntimeException and must be caught or declared. Unchecked exceptions extend RuntimeException or Error and need no declaration, though they may still be caught.
- Multi-catch constraint
- catch (A | B e) handles several unrelated exception types with one block. The parameter is implicitly final, and the alternatives must not be subclasses of one another.
- try-with-resources close order
- Resources declared in try-with-resources are closed automatically in reverse order of declaration. Only types implementing AutoCloseable qualify, and close exceptions are added as suppressed exceptions.
- Assertion enabling flag
- Assertions are disabled by default and are enabled with -ea or -enableassertions on the java command. A failed assert throws AssertionError, so assertions must never enforce public method argument checks.
- Immutable collection factories
- List.of, Set.of, and Map.of return unmodifiable collections that reject null elements and duplicate keys. Any mutation attempt throws UnsupportedOperationException, and Set.of order is unspecified.
- HashMap null policy
- HashMap permits one null key and any number of null values. Hashtable and ConcurrentHashMap reject null keys and null values, throwing NullPointerException on insertion.
- equals and hashCode contract
- Objects that are equal by equals must return identical hashCode values. Violating this makes keys unreachable in HashMap and HashSet, because lookup uses the hash bucket first.
- Iterator remove rule
- Iterator.remove deletes the last element returned by next and is the safe way to modify a collection during iteration. Calling remove before next throws IllegalStateException.
- Stream laziness
- Intermediate stream operations such as filter and map are lazy and run only when a terminal operation such as collect or count executes. A stream can be consumed only once.
- Effectively final capture
- A lambda or local class may capture local variables only if they are final or effectively final. Captured instance fields are allowed because they belong to the enclosing object.
- toMap duplicate keys
- Collectors.toMap throws IllegalStateException when two elements map to the same key. Supplying a merge function as the third argument resolves the conflict instead of throwing.
- Optional get behavior
- Calling get on an empty Optional throws NoSuchElementException. Prefer orElse, orElseGet, orElseThrow, or ifPresent to handle absence without an exception.
- requires transitive
- requires transitive makes a dependency readable by any module that reads the declaring module, effectively re-exporting it. Plain requires keeps the dependency private to the declaring module.
- Unnamed module behavior
- Code on the classpath belongs to the unnamed module, which reads every named module and exports all its packages. Named modules cannot read the unnamed module unless --add-reads is used.
- Automatic module naming
- A JAR placed on the module path without module-info becomes an automatic module. Its name derives from the JAR filename, it requires all resolved modules, and it exports all packages.
- volatile visibility guarantee
- volatile ensures that writes to a field are visible to other threads and prevents reordering around it. It does not make compound operations such as increment atomic, so synchronization or atomics are still needed.
- ExecutorService shutdown difference
- shutdown stops accepting new tasks but lets submitted tasks finish. shutdownNow attempts to interrupt running tasks and returns the list of tasks that never started.
- Files.walk stream closing
- Files.walk and Files.list return a Stream of Path that holds an open directory handle. It must be closed, typically with try-with-resources, or the underlying file descriptor leaks.
- SecureRandom versus Random
- java.util.Random produces a predictable sequence unsuitable for secrets. SecureRandom draws from a cryptographically strong generator and must be used for tokens, salts, and keys.
- PreparedStatement injection defense
- A PreparedStatement binds user input as typed parameters rather than concatenating it into SQL text. This prevents SQL injection because input is never parsed as SQL syntax.
- SimpleDateFormat thread safety
- SimpleDateFormat is mutable and not thread-safe, so sharing one instance across threads corrupts results. DateTimeFormatter is immutable and thread-safe and should be preferred.
- ResourceBundle lookup
- ResourceBundle.getBundle selects the most specific properties file matching the locale, falling back to the default bundle. A missing base bundle throws MissingResourceException at runtime.
- RUNTIME retention meaning
- An annotation with RetentionPolicy.RUNTIME is stored in the class file and exposed to reflection. SOURCE is discarded by the compiler, and CLASS is stored but invisible at runtime.
- Functional interface definition
- A functional interface declares exactly one abstract method, excluding default and static methods and methods inherited from Object. Lambdas and method references can implement it.