DEV Community

jguo
jguo

Posted on

Java functional vs OO programing

Hi Java guys,
I want to know how you guys use java. My previous job still uses OOP (except simple lambda and stream). Now, I joined Oracle. I found they use functional programing almost everywhere. How do you guys do your work?

Note, when I say functional programming, I am not just talking about simple lambda and stream functions. I especially reference if you use functions in the return type and method parameters.

During my previous job, I tried to avoid using functions in returns and method parameters. Since there are saying that function is not performing that well. I still think that is probably true. Functions are compiled to anonymous classes in behind.

For example,

Functional:

public Supplier<String> someMethod() {
return () -> "Hello!"
}

or

callMethod(() -> "Hello!")
Enter fullscreen mode Exit fullscreen mode

More OOP style.

private Supplier<String> hello = () -> "Hello!";

public Supplier<String> someMethod() {
return hello;

or 

callMethod(hello);

Enter fullscreen mode Exit fullscreen mode

Let me know your ideas.

Top comments (2)

Collapse
 
sarahk profile image
Sarah •

If by functional programming you mean the new lib introduced in Java 8 - yes I use it all the time. But you have to still use OOP, are you saying the two are mutally exclusive?

I use lambdas, method references, Consumer, Supplier, Function, Optional types, Streams (collect, reduce, map, filter, etc) and I'm not aware of any performance issues.

Collapse
 
jiayanguo profile image
jguo • • Edited

I am not saying they are mutually exclusive. Let's use an example.

Functional:

public Supplier<String> someMethod() {
return () -> "Hello!"
}

or

callMethod(() -> "Hello!")

More OOP style.

private Supplier<String> hello = () -> "Hello!";

public Supplier<String> someMethod() {
return hello;

or 

callMethod(hello);