In: Computer Science
Does Java support referential transparency? What about in static contexts like a? What about in object contexts like b? Explain using the following bits of code to explain your answer for each question.
a)
b)
Q) Does Java support referential transparency?
A) Yes, in java functional programming, we make use of
PURE functions which will not have any side effect on the program.
In referential transparency, some block of code does not affect the
program's functioning when it return a different value. Therefore,
it is an independent block of code which would not affect the
functioning of the program.
Ex-> Utility methods to format amount or date.
Q) What about in static contexts like a?
A) In case of static context, the variables are bound to
the class and not to the object and will share a common property
across all the objects of the same class.
Here, we can replace temp = fun(a) with a value
that the fun function returns corresponding to value of a. Thus,
referential integrity is maintained.
Here, the fun method is a PURE function which returns some
value based on the input
Q) What about in object contexts like
b?
A) In case of object context, we never know what the
values of a.fun() can be. As it is the method bound to the object,
its value cannot be changed with some same integer. Because,
a.fun() is not a PURE function. Calling a.fun()
everytime will not give us the same result.
Thus, referential integrity is NOT maintained
here
- Kindly upvote if
this helped