Java 8 Flashcards

(54 cards)

1
Q

[JAVA 8] What are the new features released in Java 8?

A

Lambda Expressions - Stream API - Date and Time API - Functional Interface - Interface Default and Static Methods - Optional - Base64 Encoding/Decoding - Nashorn JavaScript Engine - Collections API Enhancements - Concurrency Enhancements - Fork/Join Enhancements - Spliterator - Internal Iteration - Type and Repeatable Annotations - Method Parameter Reflection - JVM Parameter Changes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

[JAVA 8] What are the main benefits of the new features introduced in Java 8?

A

Functional programming via Lambda and Streams - high volume data processing - parameter names via Reflection - reusable Collection APIs - smart null handling with Optional - JVM control with new parameters - enhanced encryption with Base64 - faster execution with Nashorn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

[JAVA 8] Why did Oracle release Java 8?

A

The main theme of Java 8 is functional programming support. Growing database sizes and multi-core CPUs required Java to efficiently handle large-scale systems. Lambda and Streams enable Big Data interaction and cloud computing where code is passed as data across servers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

[JAVA 8] MEMORY BOOSTER: Java 8 key themes

A

Java 8 = Functional Programming. Top 5 features to know: Lambda (pass code as data) + Streams (data pipeline) + Optional (null safety) + Default Methods (backward compatibility) + new Date/Time API (thread-safe immutable dates)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

[JAVA 8] What is a Lambda expression in Java 8?

A

A Lambda expression is an anonymous function with no access modifier - no name and no return type declaration. It accepts parameters and returns a result. It can be passed as a parameter to a method treating code as data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[JAVA 8] What are the three main parts of a Lambda expression?

A

1) Parameter list (zero or more parameters). 2) Arrow operator -> separating parameters from body. 3) Body containing the code to execute. Example: e -> System.out.println(e) has parameter=e and arrow=-> and body=println

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

[JAVA 8] What is the data type of a Lambda expression?

A

The data type of a Lambda expression is a Functional Interface. In most cases this is java.lang.Runnable. The lambda provides the implementation of the single abstract method of the functional interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

[JAVA 8] What are the advantages of a Lambda expression?

A

A lambda can be passed as an object to a method reducing overhead of anonymous classes. It enables passing a method as a parameter to another method. It makes code more concise and readable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

[JAVA 8] What is the target type of a Lambda expression?

A

The target type is the Functional Interface type to which the lambda can be converted. The lambda must match the parameter types and return type of the single abstract method of that interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

[JAVA 8] What is the type of a Lambda expression in Java 8?

A

A lambda does not have a type of its own. Its type depends on the context it is used in. Generally a lambda is an instance of a Functional Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

[JAVA 8] MEMORY BOOSTER: Lambda expressions

A

Lambda = anonymous function = (params) -> body. No name + no modifier + no return type. Type = Functional Interface. Pass code as data. Short form of anonymous inner class for single-method interfaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

[JAVA 8] What is a Functional interface in Java 8?

A

A Functional interface has exactly one abstract method. It can have default and static methods with implementation. Popular examples are Runnable and Callable. It serves as the data type for Lambda expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

[JAVA 8] What is a Single Abstract Method (SAM) interface?

A

SAM interface is another name for Functional Interface. It has exactly one abstract method. The term SAM highlights that there is only one method that must be implemented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

[JAVA 8] How can we define a Functional interface in Java 8?

A

Create an interface with exactly one abstract method. Optionally annotate it with @FunctionalInterface which makes the compiler enforce the one-abstract-method rule. Overriding Object class methods does not count as an abstract method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

[JAVA 8] Is @FunctionalInterface annotation mandatory?

A

No - it is not mandatory. But using it is recommended because the compiler will then give an error if you accidentally add a second abstract method to the interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

[JAVA 8] Why do we need Functional interfaces in Java?

A

Functional interfaces are used in Lambda expressions - Method references and Constructor references. They serve as the data type for lambdas enabling functional programming style where code can be passed as data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

[JAVA 8] What are Predicate - Supplier and Consumer in Java 8?

A

Predicate = takes one argument + returns boolean result. Supplier = takes no argument + returns a result. Consumer = takes one argument + returns no result. All three are built-in functional interfaces in java.util.function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

[JAVA 8] MEMORY BOOSTER: Functional Interface

A

Functional Interface = exactly ONE abstract method = SAM. @FunctionalInterface enforces this at compile time. Lambda provides the implementation. Predicate (in+boolean out) - Supplier (nothing in+out) - Consumer (in+nothing out)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

[JAVA 8] What are the main differences between Collection and Stream API in Java 8?

A

Collection = stores data + finite elements + eager construction + multiple iterations. Stream = computes data + can be infinite + lazy construction + single consumption only. Collection is from Java 1.2. Stream is from Java 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

[JAVA 8] What are the main uses of Stream API in Java 8?

A

Declarative data processing - multi-core utilization without multi-threading code - pipeline of sequential or parallel operations - group by and order by support - functional programming style - parallel data processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

[JAVA 8] What are the differences between Intermediate and Terminal Operations in Streams?

A

Intermediate: lazy + output is another Stream + can be chained + multiple per statement. Terminal: eager + output is not a Stream + cannot be chained + only one per statement. Intermediate operations only execute when a Terminal operation is reached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

[JAVA 8] Can we consume a Stream more than once?

A

No - a Stream can only be consumed or iterated once. After a terminal operation is called the Stream is exhausted. To process the data again a new Stream must be created

23
Q

[JAVA 8] MEMORY BOOSTER: Stream API

A

Stream = pipeline + lazy + single use. Collection = container + eager + reusable. Intermediate ops = filter/map/sorted (lazy - chainable). Terminal ops = collect/forEach/count (eager - one only). Think of Stream as an assembly line that runs only when the final step is called

24
Q

[JAVA 8] What is a Spliterator in Java 8?

A

Spliterator is a special Iterator for traversing and partitioning elements of a source (Collection - IO channel or generator). It can traverse elements individually or in bulk and supports parallel as well as sequential iteration

25
[JAVA 8] What are the differences between Iterator and Spliterator?
Spliterator works with Streams. Iterator works only with Collections. Spliterator uses internal iteration. Iterator uses external iteration. Spliterator supports parallel and sequential traversal and bulk operations. Iterator only supports sequential one-by-one traversal
26
[JAVA 8] MEMORY BOOSTER: Spliterator
Spliterator = split + iterate. Designed for Streams and parallel processing. Can split source into parts for parallel processing. More powerful than Iterator: supports bulk - parallel and internal iteration
27
[JAVA 8] What is Internal Iteration in Java 8?
In Internal Iteration the Iterator controls the iteration not the collection. The client passes an operation to the Iterator and the Iterator applies it to all elements. It is easier to implement and supports parallel processing
28
[JAVA 8] What are the main differences between Internal and External Iterator?
Internal: Iterator controls iteration + declarative style + supports bulk and parallel + more readable. External: Collection controls iteration + imperative style + sequential only + one element at a time
29
[JAVA 8] What is the main disadvantage of Internal Iteration?
The developer has no control over the iteration process since the Java API handles it. You cannot pause - skip or control the order of iteration like you can with an External Iterator
30
[JAVA 8] MEMORY BOOSTER: Internal vs External Iteration
External = you control the loop (for loop - Iterator). Internal = API controls the loop (forEach - Streams). Internal is declarative: tell WHAT to do not HOW. External is imperative: tell HOW step by step. Streams use internal iteration
31
[JAVA 8] What is a Default Method in a Java 8 Interface?
A Default method has a concrete implementation inside an interface marked with the default keyword. Any class implementing the interface inherits this default behavior without needing to override it
32
[JAVA 8] Why do we need Default methods in Java 8 Interfaces?
Default methods provide backward compatibility. When a new method is added to an existing interface all implementing classes would break. A default implementation prevents this by providing fallback behavior for classes that do not override the method
33
[JAVA 8] What is the purpose of a Static method in a Java 8 Interface?
Static interface methods are utility or helper methods. They avoid the need for a separate Utils class - keep behavior encapsulated in one place and are called using the interface name not an instance
34
[JAVA 8] Can we create a class implementing two interfaces with default methods of the same name?
No - this causes a compile-time error for duplicate default methods. The class must override the method and explicitly specify which interface implementation to use via InterfaceName.super.methodName()
35
[JAVA 8] How does Java 8 solve the Diamond Problem?
When a class inherits the same default method from two interfaces it must override the method and call the desired implementation explicitly using InterfaceName.super.methodName(). This resolves the compiler ambiguity
36
[JAVA 8] What are the differences between an interface with default method and an abstract class?
Interface: no instance variables + no constructor + only default/static concrete methods + can be used for lambda. Abstract class: can have instance variables + constructors + any concrete methods + cannot be used for lambda
37
[JAVA 8] If a class extends a base class and implements an interface with the same default method which wins?
The base class definition wins. JVM always picks the class implementation over the interface default method
38
[JAVA 8] MEMORY BOOSTER: Default and Static interface methods
Default = instance method with body in interface + keyword default + inheritable + overridable. Static = utility method in interface + call via InterfaceName.method(). Both added in Java 8. Class always beats interface default. Interface A beats Interface B if A is more specific
39
[JAVA 8] What is Optional in Java 8?
Optional is a container object that may hold a null or non-null value. Use isPresent() to check if a value exists and get() to retrieve it. It is used to avoid NullPointerException and make null handling explicit
40
[JAVA 8] What are the uses of Optional in Java 8?
Avoid NullPointerException - perform null checks at compile time instead of runtime - reduce unnecessary null checks in code - provide default values for null cases using orElse() or orElseGet()
41
[JAVA 8] Which method in Optional provides a fallback for null values?
orElseGet() provides a fallback mechanism. If the Optional contains null - orElseGet() invokes the provided Supplier to return a default value. orElse() returns a direct default value
42
[JAVA 8] MEMORY BOOSTER: Optional
Optional = null-safe wrapper. isPresent() = has value? get() = retrieve value. orElse(default) = value or fallback. orElseGet(supplier) = value or computed fallback. ifPresent(consumer) = do something only if value exists. Replaces explicit null checks
43
[JAVA 8] What are the core ideas behind the Java 8 Date/Time API?
Three core ideas: Immutable value classes (thread-safe) + Domain-driven design (classes model real date/time concepts) + Separation of chronologies (supports non-ISO calendar systems like Japanese or Thai)
44
[JAVA 8] What are the advantages of the Java 8 Date/Time API over the old Date API?
Thread-safe immutable classes - better API design (months 1-12 not 0-11) - no need for third-party libraries like Joda Time - clearer domain model - better performance
45
[JAVA 8] What are the main differences between legacy Date API and Java 8 Date/Time API?
Old: not thread-safe + mutable + poor performance + months 0-11. New: thread-safe + immutable + better performance + months 1-12 + clear readable API
46
[JAVA 8] How can we get duration between two dates in Java 8?
Use the Duration class: Duration.between(date1 - date2) returns the time period between two dates in days - hours - minutes or seconds
47
[JAVA 8] How can we get the current time using the Java 8 Date/Time API?
Use the Clock class: Clock.systemDefaultZone().millis() for milliseconds or Clock.systemDefaultZone().instant() for a human-readable format. Replaces the old System.currentTimeMillis()
48
[JAVA 8] MEMORY BOOSTER: Java 8 Date/Time API
Key classes: LocalDate (date only) - LocalTime (time only) - LocalDateTime (both) - ZonedDateTime (with timezone) - Duration (time between instants) - Clock (current time). All immutable + thread-safe. Months are 1-12
49
[JAVA 8] What new Array methods were introduced in Java 8 for multi-core machines?
Java 8 added parallel prefix methods to the Arrays class: Arrays.parallelSort() - Arrays.parallelSetAll() and Arrays.parallelPrefix(). These methods leverage multiple CPU cores for fast array processing
50
[JAVA 8] What are the popular annotations introduced in Java 8?
@FunctionalInterface marks an interface as a Functional Interface and enforces single abstract method at compile time. @Repeatable marks an annotation as one that can be applied multiple times to the same element
51
[JAVA 8] What is StringJoiner in Java 8?
StringJoiner is a new class in Java 8 for building strings with a delimiter and optional prefix/suffix. new StringJoiner(":" - "[" - "]").add("One").add("Two").toString() produces [One:Two]
52
[JAVA 8] What are the new JVM arguments introduced in Java 8?
PermGen space was removed and replaced with MetaSpace. JVM options -XX:PermSize and -XX:MaxPermSize are replaced by -XX:MetaSpaceSize and -XX:MaxMetaspaceSize. MetaSpace grows dynamically by default
53
[JAVA 8] What is jdeps in Java 8?
jdeps is a new command-line tool in Java 8 for analyzing class-level and package-level dependencies. Pass a class name or jar file to get a full list of its dependencies
54
[JAVA 8] MEMORY BOOSTER: Java 8 JVM changes
PermGen OUT - MetaSpace IN. PermGen had fixed size and caused OutOfMemoryError. MetaSpace uses native memory and grows dynamically. New flags: -XX:MetaSpaceSize and -XX:MaxMetaspaceSize. jdeps tool for dependency analysis