Week 6 Flashcards
What is a functional interface in Java?
A functional interface is an interface that contains only one abstract method. It may have multiple default or static methods but should have just one abstract method to be functional. It’s often used as a target for lambda expressions or method references.
How do functional interfaces differ from regular interfaces?
Functional interfaces have exactly one abstract method, allowing them to be implemented by lambda expressions. Regular interfaces can have multiple abstract methods, making them unsuitable for lambda expressions.
Can you give an example of a built-in functional interface in Java?
One example is java.util.function.Predicate, which represents a boolean-valued function of one argument.
How can you implement a functional interface using a lambda expression?
A functional interface can be implemented using a lambda like this:
Predicate<Integer> isEven = (x) -> x % 2 == 0;</Integer>
What is the purpose of the @FunctionalInterface annotation?
This annotation is used to indicate that an interface is intended to be a functional interface. It also ensures that the compiler will generate an error if the interface has more than one abstract method.
What is a default method in a functional interface?
A default method is a method in an interface that has an implementation. It is not abstract, so it does not need to be implemented by a class that uses the interface.
Can a functional interface extend another functional interface?
Yes, a functional interface can extend another functional interface, provided that it does not add any new abstract methods.
What is the significance of the java.util.function package?
This package contains functional interfaces commonly used for lambda expressions and method references, such as Predicate, Consumer, Function, etc.
What is a lambda expression in Java?
A lambda expression is a short block of code that takes parameters and returns a value. Lambda expressions are used primarily to define the behavior of functional interfaces.
Can you describe the syntax of a lambda expression?
The basic syntax is
(parameters) -> expression
or
(parameters) -> { statements }
How does a lambda expression differ from an anonymous inner class?
Lambda expressions are more concise and focus on defining a method’s behavior, while anonymous inner classes require more boilerplate code. Also, lambdas do not have access to this in the same way that anonymous inner classes do.
What are the advantages of using lambda expressions?
Conciseness and readability
Facilitation of functional programming
Encourages the use of higher-order functions
In what scenarios would you use lambda expressions?
Lambda expressions are useful in scenarios like passing behavior as a parameter (e.g., sorting, filtering, or event handling).
What is a lambda expression’s target type?
The target type of a lambda expression is the type of the functional interface it is assigned to.
Can a lambda expression throw checked exceptions?
Yes, but the functional interface’s method must declare the exception in its signature.
How does a method reference differ from a lambda expression?
A method reference is more concise when the method already exists, while a lambda expression defines new behavior inline.
What is a method reference in Java?
A method reference is a shorthand notation of a lambda expression that refers to a method by name. It allows existing methods to be passed as lambda expressions.
What are the different types of method references?
Static method reference: ClassName::staticMethod
Instance method reference of a particular object: instance::method
Instance method reference of an arbitrary object of a particular type: ClassName::method
Constructor reference: ClassName::new
Can you provide an example of a static method reference?
Function<String, Integer> parseInt = Integer::parseInt;
How do you create an instance method reference?
If you have an instance of a class, you can reference its method like this:
MyClass obj = new MyClass();
Supplier<String> methodRef = obj::someMethod;</String>
What is the syntax for using a constructor reference?
Supplier<MyClass> constructorRef = MyClass::new;</MyClass>
What does REST stand for?
REST stands for Representational State Transfer.
What are the key principles of RESTful architecture?
Statelessness
Client-server separation
Uniform interface
Resource-based interaction
Cacheability
Layered system
How does REST differ from SOAP?
REST is simpler, stateless, and typically uses JSON or XML over HTTP, while SOAP is a protocol that uses XML messaging and provides more features like built-in error handling.
What are the common HTTP methods used in REST?
GET: Retrieve data
POST: Create new data
PUT: Update data
DELETE: Remove data
What is the purpose of a resource in REST?
A resource represents a piece of data that can be accessed and manipulated using RESTful API endpoints.
Can you explain the concept of statelessness in REST?
Each request from the client to the server must contain all the information needed to understand and process the request, as no session state is stored on the server.
What is Javalin, and what are its primary use cases?
Javalin is a lightweight web framework for Java and Kotlin, primarily used to build RESTful APIs and web applications.