Java Reference
In-Depth Information
Declarative Paradigm
In the declarative paradigm, a program consists of the description of a problem and the computer finds the solution.
The program does not specify how to arrive at the solution to the problem. It is the computer's job to arrive at a
solution when a problem is described to it. Contrast the declarative paradigm with the imperative paradigm. In the
imperative paradigm, we are concerned about the “how” part of the problem. In the declarative paradigm, we are
concerned about the “what” part of the problem. We are concerned about what the problem is, rather than
how to solve it. The functional paradigm and the logic paradigm, which are described next, are subtypes of the
declarative paradigm.
Writing a database query using a structured query language (SQL) falls under programming based on the
declarative paradigm where you specify what data you want and the database engine figures out how to retrieve the
data for you. Unlike the imperative paradigm, the data is permanent and the algorithm is transient in the declarative
paradigm. In the imperative paradigm, the data is modified as the algorithm is executed. In the declarative paradigm,
data is supplied to the algorithm as input and the input data remains unchanged as the algorithm is executed. The
algorithm produces new data rather than modifying the input data. In other words, in the declarative paradigm,
execution of an algorithm does not produce side effects.
Functional Paradigm
The functional paradigm is based on the concept of mathematical functions. You can think of a function as an
algorithm that computes a value from some given inputs. Unlike a procedure in procedural programming, a function
does not have a side effect. In functional programming, values are immutable. A new value is derived by applying a
function to the input value. The input value does not change. Functional programming languages do not use variables
and assignments, which are used for modifying data. In imperative programming, a repeated task is performed using
a loop construct, for example, a while loop. In functional programming, a repeated task is performed using recursion,
which is a way in which a function is defined in terms of itself. In other words, it does some work, then calls itself.
A function always produces the same output when it is applied on the same input. A function, say add , that can
be applied to an integer x to add an integer n to it may be defined as follows:
int add(x, n) {
if (n == 0) {
return x;
}
else {
return 1 + add(x, n-1); // Apply add function recursively
}
}
Note that the add function does not use any variable and does not modify any data. It uses recursion. You can call
the add function to add 10 to 15 as follows:
add(15, 10); // Results in 25
Haskell, Erlang, and Scala are a few examples of programming languages that support the functional paradigm.
Java 8 added a new language construct called a lambda expression, which can be used to perform functional
programming in Java.
Tip
 
 
Search WWH ::




Custom Search