OCAJP 1.8, OCPJP 1.8, OCMJEA Part1,2&3
Monica Shiralkar wrote:Isnt the first code more self explanatory.
SwapanB Biswas wrote:So one needs much higher level of knowledge to understand code with functional programming.
What's more self explanatory about it? It doesn't explain anything more than the second version does. It's just more verbose.
Why do you need to know anything about what sort of object you are creating?
Monica Shiralkar wrote:
Isnt it required to learn that which Interface we are supposed to implement?
I think, in this case, its better to think of function signatures than to think of which interfaces to implement. Remember we are in the realm lambdas and functional programming here.
Monica Shiralkar wrote:
I think, in this case, its better to think of function signatures than to think of which interfaces to implement. Remember we are in the realm lambdas and functional programming here.
Ok, in that case what all is the exact understanding from this line ?
Monica Shiralkar wrote:Ok, in that case what all is the exact understanding from this line ?
Stephan van Hulst wrote:That's just the terminology.
In fact, in many functional languages, functions ARE objects and all objects are functions.
It's helpful to think of functions (note that I didn't say methods) as objects.
You can think of a function in Java as any object that implements a functional interface(or objects).
prefer to think what I am putting in and what I am getting out. If I get it wrong, it won't compile.
Monica Shiralkar wrote:Does it mean that a java method is known as functions only if it is method of some functional interface.
Object that implements a functional interface or Object of a class that implements a functional interface?
This can be used only in case of functional interface but while trying to use it how to relate it to the functional interface.
I don't know what you mean. Can you reformulate your question?
lines.methodX(line -> Arrays.asList(line.split(" ")));
That doesn't always work. This well‑known interface from GUI event handling is a functional interface, but isn't tagged with the @FunctionalInterface annotation.Stephan van Hulst wrote:. . . seeing if it is annotated with @FunctionalInterface. . . .
Monica Shiralkar wrote:So that means there are only limited situations where Functional programming can be used in Java. We can use functional programming Only in the cases where a method can take an instance of functional interface as the argument.
Campbell Ritchie wrote:Not at all.
You can easily write a method as a pure function.
Not quite. The λ expression creates an instance, but by a different route from that for creating an anonymous class. Try it out in JShell:-Monica Shiralkar wrote:. . . Interfaces (functional ones) are involved and passing of their instance to a method is to be replaced with passing of a lambda expression to the method.
Note that I have corrected the indentation, and omitted quite a lot of the output.Campbell's JShell wrote: [campbell@localhost ~]$ jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro
. . . .
ToIntFunction<String> lengthGetter = s -> s.length();
lengthGetter ==> ???
. . . .
jshell> ToIntFunction<String> lengthGetter = new ToIntFunction<>()
...> {
...> @Override public int applyAsInt(String s)
...> {
...> return s.length();
...> }
...> };
lengthGetter ==> ???
Consider Paul's rocket mass heater. |