[JAVA 8] What are the new features released in Java 8?
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
[JAVA 8] What are the main benefits of the new features introduced in Java 8?
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
[JAVA 8] Why did Oracle release Java 8?
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
[JAVA 8] MEMORY BOOSTER: Java 8 key themes
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)
[JAVA 8] What is a Lambda expression in Java 8?
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
[JAVA 8] What are the three main parts of a Lambda expression?
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
[JAVA 8] What is the data type of a Lambda expression?
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
[JAVA 8] What are the advantages of a Lambda expression?
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
[JAVA 8] What is the target type of a Lambda expression?
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
[JAVA 8] What is the type of a Lambda expression in Java 8?
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
[JAVA 8] MEMORY BOOSTER: Lambda expressions
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
[JAVA 8] What is a Functional interface in Java 8?
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
[JAVA 8] What is a Single Abstract Method (SAM) interface?
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
[JAVA 8] How can we define a Functional interface in Java 8?
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
[JAVA 8] Is @FunctionalInterface annotation mandatory?
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
[JAVA 8] Why do we need Functional interfaces in Java?
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
[JAVA 8] What are Predicate - Supplier and Consumer in Java 8?
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
[JAVA 8] MEMORY BOOSTER: Functional Interface
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)
[JAVA 8] What are the main differences between Collection and Stream API in Java 8?
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
[JAVA 8] What are the main uses of Stream API in Java 8?
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
[JAVA 8] What are the differences between Intermediate and Terminal Operations in Streams?
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
[JAVA 8] Can we consume a Stream more than once?
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
[JAVA 8] MEMORY BOOSTER: Stream API
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
[JAVA 8] What is a Spliterator in Java 8?
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