Java Functional Programming Q&A (Need to know about Java Q&A Article Series 3)
Hi Java people.
I am Huseyin. Have been a .Net developer for years and Java backend and Android developer for 6 years as well as a QA Automation Engineer team member for several projects for 3 years.
Have been getting a reasonable amount of questions from any level of Java developers for years. In the end, decided to write my own answers instead of spending my time over the same thing again and again. As a developer, by experiencing the reusability, abstraction and the maintainability issues for years, I decided to implement the same structure in my life and abstract myself from answering similar questions 🙂. Thus, put my effort into creating this “Need to know about Java Q&A Article Series”. By doing so, I am comfortable about answering similar questions by just sending a link to these articles 🙂. It helped me to understand many very important concepts of Java as well.
The target audience of “Need to know about Java Q&A Article Series” is the ones who already know coding with Java, but also want to improve his/her knowledge or to prepare for Java Interviews. If you are a new bee, I believe these articles may be a couple of levels higher for you.
Main purpose of these articles is not to explain topics in detail. Of course there are a lot of sources about Java on the internet. What I experienced is that some different approaches while explaining the same thing helps people to understand the topics. What I am trying to do is actually that. A different kind of explanation.
Articles are prepared by Question & Answer structure which I do believe is an efficient way of learning.
If these articles help you to understand a concept, I definitely feel very happy. Feel free to share and comment.
Sharing is learning at the same time! Enjoy.
What is behavior parameterization? Why is it an important concept in software development?
Behavior parameterization is a software development pattern that lets you handle frequent requirement changes. In a nutshell, it means taking a block of code and making it available without executing it. This block of code can be called later by other parts of your programs, which means that you can defer the execution of that block of code. For instance, you could pass the block of code as an argument to another method that will execute it later. As a result, the method’s behavior is parameterized based on that block of code. It can be achieved by abstract methods. Either concrete implementations or unanimous implementations may be used as well as lambda expressions.
Behavior parameterization minimizes engineering efforts and enables handling requirement changes easily, thus enhancing maintainability of the framework.
How can OOP concepts be implemented in behavior parameterization?
Since behaviors are methods and Behavior Parameterization depends on multiple implementations of methods, we should definitely apply the Polymorphism concept. Moreover, since we are interested in different implementations of abstract methods, Abstraction is the key concept for Behavior Parameterization.
In a simple Behavior Parameterization implementation, we can use an Interface as the reference type and different implementations of the abstract methods, which belong to that interface, as the object type.
So, Polymorphism, Inheritance and Abstraction concepts help us to implement the Behavior Parameterization Pattern.
What is Functional Programming? Explain the difference from OOP.
Functional Programming is a paradigm of building computer programs using expressions and functions without mutating state and data.
In Functional Programming, functions are treated as primitives. Primitives serve as the simplest elements of a programming language. Thus, a function can be stored in a variable, passed as an argument, and returned from a function.
Some programming languages only allow for one particular paradigm like Functional Programming or Object Oriented Programming. On the other hand, some languages, like Java, provide both of the paradigms.
At its simplest, functional programming uses immutable data to tell the program exactly what to do, while Object-oriented programming tells the program how to achieve results through objects altering the program’s state. Both paradigms can be used solely to create elegant code. It is also possible to use both programming paradigms together in the same framework, according to our own needs.
What is Lambda Expression? What are the certain features of Lambda Expression?
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method (Functional Interface), then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you’re usually trying to pass functionality as an argument to another method. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.
Lambda Expressions included in Java at SE 8.
In case of lambda expression, we don’t need to define the method again for providing the implementation. Here, we just write the implementation code. Thus, it provides less coding for in-place Functional Interface implementation.
Java lambda expression consists of (1) Argument-list, (2) Arrow-token, and (3) Body. If there is a one line implementation, Lambda Expressions doesn’t need curly braces and return statements as well.
How is a Lambda Expression written?
Java lambda expression is treated as a function, so the compiler does not create a .class file. For a Functional Interface implementation with Lambda Expressions, it is just needed to create parentheses, if parameters are needed just name them in order without writing their data types, add dash and greater symbols, then write the implementation without return keyword, even if the method is not void. If there will be multiple expressions in the implementation then just wrap them in curly braces.
Since functional interfaces have only one abstract method, the compiler is aware of the method behavior and does not need the name of the method and the parameter data types any more.
What is Functional Interface? Examples of the ready Java Functional interfaces?
Before Java 8, we would usually create a class for every case where we needed to encapsulate (implement) a single piece of functionality. This implied a lot of unnecessary boilerplate code to define something that served as a primitive function representation. There was no chance to present functions solely. Functions were part of a class, and if someone wants to use them, they have to use the class or object of the class to call any function until Java 8.
With Java 8, any interface with a Single Abstract Method is a functional interface, and its implementation may be treated as lambda expressions. By this way, there is no need to create a concrete implementation class. Rather than that, implementation can be done in place. Some built-in Java Functional Interfaces are;
(1) Function Interface, accepts an object of any data type and returns a result of any datatype by performing apply method
(2) Supplier Interface, does not accept any arguments and returns an object of any data type by performing get method
(3) Consumer Interface, accepts a single argument of any data type and does not return any result, performing accept method
(4) Predicate Interface, accepts a single argument of any data type and returns boolean data type by performing test method.
Custom Functional Interfaces can be declared as well. Keep in mind that a functional interface can have any number of default methods.
What is Lambda Expression syntax?
A Java lambda expression is a function which can be created without belonging to any class. A Java lambda expression can be passed around as if it was an object and executed on demand.
A lambda expression with no parameter and single expression can be declared like that;
() -> expression
A lambda expression with multiple parameters and single expression can be declared like that;
(param1, param2) -> expression
A lambda expression with a code block can be declared like that;
(parameter1, parameter2) -> { code block }
In this case if the function has return type, then return keyword should be used. But, for a single expression it is not needed.
What are the various forms of writing Lambda expressions?
A lambda expression with no parameter and single expression can be declared like that;
() -> expression
Example:
Supplier<LocalDateTime> currentTime = () -> LocalDateTime.now();
A lambda expression with multiple parameters and single expression can be declared like that;
(param1, param2) -> expression
Example:
BiFunction<Integer, Integer, Integer> sumUp = (x1, x2) -> x1 + x2;
A lambda expression with a code block can be declared like that;
(parameter1, parameter2) -> { code block }
Example:
BiFunction<Integer, Integer, Double> sumUp = (x1, x2) -> { int sum = x1 + x2; return Math.pow(sum, sum);};
Thanks for reading. Hope this helps! See you in the next articles.
PS: Really benefited from especially Baeldung and other Java Tutorial Websites, as well as Official Java Documentation.
Dedicated to Dirty Java Rangers