Java SE 8 Programmer II study materials
Java SE 8 Programmer II study resources

Java SE 8 Programmer II flashcards

Java SE 8 Programmer II · 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.

Method overriding visibility rule
An override may not reduce visibility; it can only widen it. public > protected > default > private. Private methods are not inherited, so a same-signature method in a subclass is a new method, not an override.
Static method hiding
A static method with the same signature in a subclass hides the superclass version rather than overriding it. The invoked version depends on the declared reference type at compile time, not on the runtime object type.
Covariant return type
An overriding method may declare a return type that is a subtype of the superclass method's return type. The compiler generates a bridge method to preserve polymorphism for callers compiled against the supertype.
Blank final variable
A final field declared without an initializer must be assigned exactly once in every constructor or in an instance initializer. Assigning it twice, or leaving a path unassigned, is a compile-time error.
Abstract class with no abstract methods
A class declared abstract may contain zero abstract methods and still cannot be instantiated. Its purpose is to prevent direct instantiation and to allow subclasses to share concrete behavior.
Interface field modifiers
Fields declared in an interface are implicitly public, static, and final regardless of written modifiers. They must be initialized at declaration, and any implementing class sees them as constants.
Anonymous inner class limits
An anonymous class cannot declare a constructor and cannot be abstract. It can access effectively final local variables and any members of the enclosing class, and it captures the enclosing instance when created in an instance context.
Enum constructor access
Enum constructors are implicitly private and cannot be invoked with new outside the enum body. Constants are the only instances, and an enum may implement interfaces but cannot extend another class.
Type erasure consequence
Generic type arguments are removed at compile time, so List<String> and List<Integer> are the same runtime class. Overloads that differ only in type parameters do not compile, and instanceof with a parameterized type is illegal.
Upper bounded wildcard
List<? extends Number> accepts List<Integer> or List<Double>. Reading yields a Number, but adding is prohibited except null, because the compiler cannot know the actual element type.
Lower bounded wildcard
List<? super Integer> accepts List<Integer>, List<Number>, or List<Object>. Adding an Integer is allowed, but reading returns Object because the compiler only knows the supertype bound.
TreeSet duplicate rule
TreeSet uses compareTo or the supplied Comparator, not equals, to detect duplicates. If compare returns zero, the second element is rejected even when equals returns false, so ordering must be consistent with equals.
Function and UnaryOperator
Function<T,R> declares R apply(T). UnaryOperator<T> extends Function<T,T>, so input and output types are the same. BinaryOperator<T> extends BiFunction<T,T,T> for two inputs of type T.
Supplier versus Consumer
Supplier<T> takes no arguments and returns T via get(). Consumer<T> takes one argument and returns void via accept(T). Both are functional interfaces usable as lambdas or method references.
Method reference forms
Four forms exist: Type::staticMethod, instance::instanceMethod, Type::instanceMethod where the first parameter becomes the receiver, and Type::new for a constructor reference. All are shorthand for a lambda.
Effectively final capture
A lambda or local class may capture a local variable only if it is effectively final, meaning assigned once and never reassigned. Fields, whether instance or static, are not subject to this restriction.
Stream single use
A stream pipeline may be consumed only once. After a terminal operation runs, any further operation on the same stream throws IllegalStateException; obtain a fresh stream from the source instead.
Intermediate versus terminal operations
Intermediate operations such as filter, map, and sorted are lazy and return a Stream. Terminal operations such as collect, forEach, and count trigger execution and return a non-stream result.
Collectors.toMap duplicate key
Collectors.toMap throws IllegalStateException when two elements map to the same key unless a merge function is supplied as the third argument, such as (a, b) -> a to keep the first value.
Optional orElse versus orElseGet
orElse always evaluates its argument, even when the Optional has a value. orElseGet invokes the Supplier only when empty, so use it when the fallback is expensive or has side effects.
Checked versus unchecked exceptions
Checked exceptions extend Exception but not RuntimeException and must be caught or declared. Unchecked exceptions extend RuntimeException or Error and need not be declared, though they may be caught.
Multi-catch restrictions
A multi-catch parameter such as catch (IOException | SQLException e) is implicitly final and cannot be reassigned. The alternatives must be disjoint; catching a type and its subclass together is a compile error.
Try-with-resources close order
Resources are closed in reverse order of declaration. If the body throws and closing also throws, the close exception is suppressed and attached to the primary exception, retrievable via getSuppressed().
Assertion enabling flag
Assertions are disabled by default and throw AssertionError only when enabled with -ea or -enableassertions. They should validate internal invariants and must not perform side effects that program logic depends on.
Transient field serialization
A transient field is skipped during serialization and restored to its default value on deserialization. Static fields are also not serialized because they belong to the class, not the instance.
BufferedReader readLine end condition
BufferedReader.readLine returns the line without the terminator, or null when the end of the stream is reached. An empty line returns an empty string, so test for null rather than length zero.
Thread start versus run
Calling start() schedules a new thread that then executes run(). Calling run() directly executes the method on the current thread, so no new thread is created and concurrency is not achieved.
Future.get exception wrapping
Future.get blocks until the task completes. If the Callable threw, get throws ExecutionException whose getCause returns the original exception; interruption throws InterruptedException, and timeout throws TimeoutException.
PreparedStatement versus Statement
PreparedStatement is precompiled with ? placeholders and set via setInt, setString, and similar methods. It prevents SQL injection through data, while Statement concatenates SQL strings and is suited to static queries.
ResultSet cursor position
A ResultSet cursor starts before the first row. next() advances and returns false when no rows remain. Column indexes are 1-based, and reading by label is case-insensitive in most drivers.
ResourceBundle fallback order
getBundle searches baseName_language_country, then baseName_language, then baseName, then the default locale variants. A missing key in a found bundle throws MissingResourceException rather than falling back to the parent bundle.
Files.walk stream closing
Files.walk and Files.list return lazy Stream<Path> holding open directory handles. They must be closed, typically with try-with-resources, or the underlying file descriptors leak until garbage collection.